Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve public/ folder when page routes are disabled #10169

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -520,12 +520,12 @@ export default class Server {
)
}

const catchAllRoute: Route = {
const catchPublicDirectoryRoute: Route = {
match: route('/:path*'),
type: 'route',
name: 'Catchall render',
name: 'Catch public directory route',
fn: async (req, res, params, parsedUrl) => {
const { pathname, query } = parsedUrl
const { pathname } = parsedUrl
if (!pathname) {
throw new Error('pathname is undefined')
}
Expand All @@ -537,6 +537,24 @@ export default class Server {
}
}

return {
finished: false,
}
},
}

routes.push(catchPublicDirectoryRoute)

const catchAllRoute: Route = {
match: route('/:path*'),
type: 'route',
name: 'Catchall render',
fn: async (req, res, params, parsedUrl) => {
const { pathname, query } = parsedUrl
if (!pathname) {
throw new Error('pathname is undefined')
}

if (params?.path?.[0] === 'api') {
const handled = await this.handleApiRequest(
req as NextApiRequest,
Expand Down
1 change: 1 addition & 0 deletions test/integration/filesystempublicroutes/public/hello.txt
@@ -0,0 +1 @@
hello
7 changes: 7 additions & 0 deletions test/integration/filesystempublicroutes/test/index.test.js
Expand Up @@ -54,4 +54,11 @@ describe('FileSystemPublicRoutes', () => {
const body = await res.text()
expect(body).toMatch(/exportpathmap was here/)
})

it('should route to public folder files', async () => {
const res = await fetch('/hello.txt')
expect(res.status).toBe(200)
const body = await res.text()
expect(body).toMatch(/hello/)
})
})