diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index 2cae6262e1d6208c7a2e39c61bd17f4bdf101cfb..50704b0e8033a4cad49ff52df691445fb36b1568 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -219,6 +219,10 @@ export default class Server { } protected generateRoutes(): Route[] { + const publicRoutes = fs.existsSync(this.publicDir) + ? this.generatePublicRoutes() + : [] + const routes: Route[] = [ { match: route('/_next/static/:path*'), @@ -270,6 +274,7 @@ export default class Server { await this.render404(req, res, parsedUrl) }, }, + ...publicRoutes, { // It's very important to keep this route's param optional. // (but it should support as many params as needed, separated by '/') @@ -294,10 +299,6 @@ export default class Server { }, ] - if (fs.existsSync(this.publicDir)) { - routes.push(...this.generatePublicRoutes()) - } - if (this.nextConfig.useFileSystemPublicRoutes) { this.dynamicRoutes = this.getDynamicRoutes() diff --git a/test/integration/basic/public/static/legacy.txt b/test/integration/basic/public/static/legacy.txt new file mode 100644 index 0000000000000000000000000000000000000000..5b8131490835184bfefabfb2319f5c37be6e4dc4 --- /dev/null +++ b/test/integration/basic/public/static/legacy.txt @@ -0,0 +1 @@ +new static folder diff --git a/test/integration/basic/test/public-folder.js b/test/integration/basic/test/public-folder.js index 5b7f1cb3c384c69b4170597724e351c8f07a400a..e64592e3bce8844c40cfe6c29236c83d43cc7114 100644 --- a/test/integration/basic/test/public-folder.js +++ b/test/integration/basic/test/public-folder.js @@ -6,6 +6,9 @@ export default context => { it('should allow access to public files', async () => { const data = await renderViaHTTP(context.appPort, '/data/data.txt') expect(data).toBe('data') + + const legacy = await renderViaHTTP(context.appPort, '/static/legacy.txt') + expect(legacy).toMatch(`new static folder`) }) }) } diff --git a/test/integration/production/public/static/legacy.txt b/test/integration/production/public/static/legacy.txt new file mode 100644 index 0000000000000000000000000000000000000000..5b8131490835184bfefabfb2319f5c37be6e4dc4 --- /dev/null +++ b/test/integration/production/public/static/legacy.txt @@ -0,0 +1 @@ +new static folder diff --git a/test/integration/production/test/index.test.js b/test/integration/production/test/index.test.js index 9a7e02f5dbadf6ec674c15e2e940d02dbcdb3f92..c2c2cff4785a5846bc6a9740d298eb7fd3d8fbac 100644 --- a/test/integration/production/test/index.test.js +++ b/test/integration/production/test/index.test.js @@ -377,8 +377,10 @@ describe('Production Usage', () => { it('Should allow access to public files', async () => { const data = await renderViaHTTP(appPort, '/data/data.txt') const file = await renderViaHTTP(appPort, '/file') + const legacy = await renderViaHTTP(appPort, '/static/legacy.txt') expect(data).toBe('data') expect(file).toBe('test') + expect(legacy).toMatch(`new static folder`) }) it('should reload the page on page script error', async () => { diff --git a/test/integration/serverless/public/static/legacy.txt b/test/integration/serverless/public/static/legacy.txt new file mode 100644 index 0000000000000000000000000000000000000000..5b8131490835184bfefabfb2319f5c37be6e4dc4 --- /dev/null +++ b/test/integration/serverless/public/static/legacy.txt @@ -0,0 +1 @@ +new static folder diff --git a/test/integration/serverless/test/index.test.js b/test/integration/serverless/test/index.test.js index 442697d24f22ddc31d4904ce9be4d9f50596d3e6..b54b7395ec5e7e12b1f97d936accdcc90b1c4fe8 100644 --- a/test/integration/serverless/test/index.test.js +++ b/test/integration/serverless/test/index.test.js @@ -56,6 +56,9 @@ describe('Serverless', () => { it('should serve file from public folder', async () => { const content = await renderViaHTTP(appPort, '/hello.txt') expect(content.trim()).toBe('hello world') + + const legacy = await renderViaHTTP(appPort, '/static/legacy.txt') + expect(legacy).toMatch(`new static folder`) }) it('should render the page with dynamic import', async () => {