Unverified Commit 681fbbd0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix basePath and public folder check ending routes early (#16356)

This corrects the basePath being required check for filesystem routes to not consider the public folder catch-all route since it always matches even if the public file isn't present and instead moves the basePath check inside of the public-folder catch-all. Tests already exist that catch this by adding a public folder to the existing `basepath` test suite

Fixes: https://github.com/vercel/next.js/issues/16332
Closes: https://github.com/vercel/next.js/pull/16350
parent 0226e787
...@@ -783,6 +783,14 @@ export default class Server { ...@@ -783,6 +783,14 @@ export default class Server {
name: 'public folder catchall', name: 'public folder catchall',
fn: async (req, res, params, parsedUrl) => { fn: async (req, res, params, parsedUrl) => {
const pathParts: string[] = params.path || [] const pathParts: string[] = params.path || []
const { basePath } = this.nextConfig
// if basePath is defined require it be present
if (basePath) {
if (pathParts[0] !== basePath.substr(1)) return { finished: false }
pathParts.shift()
}
const path = `/${pathParts.join('/')}` const path = `/${pathParts.join('/')}`
if (publicFiles.has(path)) { if (publicFiles.has(path)) {
......
...@@ -37,6 +37,7 @@ export type PageChecker = (pathname: string) => Promise<boolean> ...@@ -37,6 +37,7 @@ export type PageChecker = (pathname: string) => Promise<boolean>
const customRouteTypes = new Set(['rewrite', 'redirect', 'header']) const customRouteTypes = new Set(['rewrite', 'redirect', 'header'])
function replaceBasePath(basePath: string, pathname: string) { function replaceBasePath(basePath: string, pathname: string) {
// If replace ends up replacing the full url it'll be `undefined`, meaning we have to default it to `/`
return pathname!.replace(basePath, '') || '/' return pathname!.replace(basePath, '') || '/'
} }
...@@ -166,9 +167,10 @@ export default class Router { ...@@ -166,9 +167,10 @@ export default class Router {
const originalPathname = currentPathname const originalPathname = currentPathname
const requireBasePath = testRoute.requireBasePath !== false const requireBasePath = testRoute.requireBasePath !== false
const isCustomRoute = customRouteTypes.has(testRoute.type) const isCustomRoute = customRouteTypes.has(testRoute.type)
const isPublicFolderCatchall = testRoute.name === 'public folder catchall'
const keepBasePath = isCustomRoute || isPublicFolderCatchall
if (!isCustomRoute) { if (!keepBasePath) {
// If replace ends up replacing the full url it'll be `undefined`, meaning we have to default it to `/`
currentPathname = replaceBasePath(this.basePath, currentPathname!) currentPathname = replaceBasePath(this.basePath, currentPathname!)
} }
...@@ -178,7 +180,7 @@ export default class Router { ...@@ -178,7 +180,7 @@ export default class Router {
if (newParams) { if (newParams) {
// since we require basePath be present for non-custom-routes we // since we require basePath be present for non-custom-routes we
// 404 here when we matched an fs route // 404 here when we matched an fs route
if (!isCustomRoute) { if (!keepBasePath) {
if (!originallyHadBasePath && !(req as any)._nextDidRewrite) { if (!originallyHadBasePath && !(req as any)._nextDidRewrite) {
if (requireBasePath) { if (requireBasePath) {
// consider this a non-match so the 404 renders // consider this a non-match so the 404 renders
...@@ -201,7 +203,7 @@ export default class Router { ...@@ -201,7 +203,7 @@ export default class Router {
// since the fs route didn't match we need to re-add the basePath // since the fs route didn't match we need to re-add the basePath
// to continue checking rewrites with the basePath present // to continue checking rewrites with the basePath present
if (!isCustomRoute) { if (!keepBasePath) {
parsedUrlUpdated.pathname = originalPathname parsedUrlUpdated.pathname = originalPathname
} }
...@@ -272,7 +274,6 @@ export default class Router { ...@@ -272,7 +274,6 @@ export default class Router {
} }
} }
} }
return false return false
} }
} }
hello world
\ No newline at end of file
...@@ -135,6 +135,17 @@ const runTests = (context, dev = false) => { ...@@ -135,6 +135,17 @@ const runTests = (context, dev = false) => {
}) })
} }
it('should 404 for public file without basePath', async () => {
const res = await fetchViaHTTP(context.appPort, '/data.txt')
expect(res.status).toBe(404)
})
it('should serve public file with basePath correctly', async () => {
const res = await fetchViaHTTP(context.appPort, '/docs/data.txt')
expect(res.status).toBe(200)
expect(await res.text()).toBe('hello world')
})
it('should rewrite with basePath by default', async () => { it('should rewrite with basePath by default', async () => {
const html = await renderViaHTTP(context.appPort, '/docs/rewrite-1') const html = await renderViaHTTP(context.appPort, '/docs/rewrite-1')
expect(html).toContain('getServerSideProps') expect(html).toContain('getServerSideProps')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment