Skip to content

Commit

Permalink
Serve public/ folder when page routes are disabled (#10169)
Browse files Browse the repository at this point in the history
Co-authored-by: JJ Kasper <jj@jjsweb.site>
Co-authored-by: Joe Haddad <timer150@gmail.com>
  • Loading branch information
3 people committed Jan 20, 2020
1 parent b95214e commit 7a728d6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
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/)
})
})

0 comments on commit 7a728d6

Please sign in to comment.