forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with afterFiles rewrites not working when prefixed with ass…
…etPrefix (vercel#68694) This PR fixes two issues with the use of `assetPrefix`: #1: vercel#64710 `assetPrefix` needs to be handled in `dev`, `deploy`, and `start`. In the current approach, only `dev` and `start` were handled, but a quirk of the implementation caused rewrites for non-asset paths to not be able to be used in `afterFiles` rewrites. vercel#2: When deploying Next.js (such as on Vercel), you need to add your own `beforeFiles` rewrite for `/${assetPrefix}/_next/...` requests or otherwise they would 404. This PR creates an automatically added `rewrite` to `beforeFiles` that handles the case for `dev`, `start`, and `deploy`, removes the existing logic in `filesystem.ts`, and adds more tests to check the behavior.
- Loading branch information
Showing
12 changed files
with
289 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import loadCustomRoutes from './load-custom-routes' | ||
|
||
describe('loadCustomRoutes', () => { | ||
describe('rewrites', () => { | ||
it('missing rewrites should not throw', async () => { | ||
const customRoutes = await loadCustomRoutes({}) | ||
expect(customRoutes.rewrites.beforeFiles).toEqual([]) | ||
expect(customRoutes.rewrites.afterFiles).toEqual([]) | ||
expect(customRoutes.rewrites.fallback).toEqual([]) | ||
}) | ||
|
||
it('array rewrites should be added to afterFiles', async () => { | ||
const customRoutes = await loadCustomRoutes({ | ||
async rewrites() { | ||
return [ | ||
{ | ||
source: '/a', | ||
destination: '/b', | ||
}, | ||
] | ||
}, | ||
}) | ||
expect(customRoutes.rewrites.beforeFiles).toEqual([]) | ||
expect(customRoutes.rewrites.afterFiles).toEqual([ | ||
{ | ||
destination: '/b', | ||
source: '/a', | ||
}, | ||
]) | ||
expect(customRoutes.rewrites.fallback).toEqual([]) | ||
}) | ||
|
||
it('rewrites should be preserved correctly', async () => { | ||
const customRoutes = await loadCustomRoutes({ | ||
async rewrites() { | ||
return { | ||
beforeFiles: [ | ||
{ | ||
source: '/beforeFiles/a', | ||
destination: '/beforeFiles/b', | ||
}, | ||
], | ||
afterFiles: [ | ||
{ | ||
source: '/afterFiles/a', | ||
destination: '/afterFiles/b', | ||
}, | ||
], | ||
fallback: [ | ||
{ | ||
source: '/fallback/a', | ||
destination: '/fallback/b', | ||
}, | ||
], | ||
} | ||
}, | ||
}) | ||
expect(customRoutes.rewrites.beforeFiles).toEqual([ | ||
{ | ||
destination: '/beforeFiles/b', | ||
source: '/beforeFiles/a', | ||
}, | ||
]) | ||
expect(customRoutes.rewrites.afterFiles).toEqual([ | ||
{ | ||
destination: '/afterFiles/b', | ||
source: '/afterFiles/a', | ||
}, | ||
]) | ||
expect(customRoutes.rewrites.fallback).toEqual([ | ||
{ | ||
destination: '/fallback/b', | ||
source: '/fallback/a', | ||
}, | ||
]) | ||
}) | ||
|
||
describe('assetPrefix', () => { | ||
it('automatically inserts assetPrefix rewrite for /_next/ paths', async () => { | ||
const customRoutes = await loadCustomRoutes({ | ||
assetPrefix: '/custom-asset-prefix', | ||
async rewrites() { | ||
return [ | ||
{ | ||
source: '/a', | ||
destination: '/b', | ||
}, | ||
] | ||
}, | ||
}) | ||
expect(customRoutes.rewrites.beforeFiles).toEqual([ | ||
{ | ||
destination: '/_next/:path+', | ||
source: '/custom-asset-prefix/_next/:path+', | ||
}, | ||
]) | ||
expect(customRoutes.rewrites.afterFiles).toEqual([ | ||
{ | ||
destination: '/b', | ||
source: '/a', | ||
}, | ||
]) | ||
}) | ||
|
||
it('automatically inserts assetPrefix rewrite for /_next/ paths when rewrites() is not present', async () => { | ||
const customRoutes = await loadCustomRoutes({ | ||
assetPrefix: '/custom-asset-prefix', | ||
}) | ||
expect(customRoutes.rewrites.beforeFiles).toEqual([ | ||
{ | ||
destination: '/_next/:path+', | ||
source: '/custom-asset-prefix/_next/:path+', | ||
}, | ||
]) | ||
expect(customRoutes.rewrites.afterFiles).toEqual([]) | ||
}) | ||
|
||
it('automatically inserts assetPrefix rewrite for /_next/ paths for basePath', async () => { | ||
const customRoutes = await loadCustomRoutes({ | ||
assetPrefix: '/custom-asset-prefix', | ||
basePath: '/custom-base-path', | ||
async rewrites() { | ||
return [ | ||
{ | ||
source: '/a', | ||
destination: '/b', | ||
}, | ||
] | ||
}, | ||
}) | ||
expect(customRoutes.rewrites.beforeFiles).toEqual([ | ||
{ | ||
destination: '/custom-base-path/_next/:path+', | ||
source: '/custom-asset-prefix/_next/:path+', | ||
}, | ||
]) | ||
expect(customRoutes.rewrites.afterFiles).toEqual([ | ||
{ | ||
destination: '/custom-base-path/b', | ||
source: '/custom-base-path/a', | ||
}, | ||
]) | ||
}) | ||
|
||
it('does not insert assetPrefix rewrite for /_next/ paths when assetPrefix is absolute URL', async () => { | ||
const customRoutes = await loadCustomRoutes({ | ||
assetPrefix: 'https://example.com/custom-asset-prefix', | ||
}) | ||
expect(customRoutes.rewrites.beforeFiles).toEqual([]) | ||
expect(customRoutes.rewrites.afterFiles).toEqual([]) | ||
}) | ||
|
||
it('does not add rewrite when assetPrefix === basePath', async () => { | ||
const customRoutes = await loadCustomRoutes({ | ||
assetPrefix: '/base', | ||
basePath: '/base', | ||
}) | ||
expect(customRoutes.rewrites.beforeFiles).toEqual([]) | ||
expect(customRoutes.rewrites.afterFiles).toEqual([]) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
test/development/basic/asset-prefix/fixture/pages/api/test-json.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function handler(req, res) { | ||
res.status(200).json({ message: 'test' }) | ||
} |
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/asset-prefix-with-basepath/app/api/test-json/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export const GET = () => { | ||
return NextResponse.json({ message: 'test' }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
test/e2e/app-dir/asset-prefix-with-basepath/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,16 @@ | ||
module.exports = { | ||
assetPrefix: '/custom-asset-prefix', | ||
basePath: '/custom-base-path', | ||
async rewrites() { | ||
return [ | ||
{ | ||
source: '/custom-asset-prefix/:path*', | ||
destination: '/:path*', | ||
}, | ||
{ | ||
source: '/not-custom-asset-prefix/:path*', | ||
destination: '/:path*', | ||
}, | ||
] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export const GET = () => { | ||
return NextResponse.json({ message: 'test' }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
module.exports = { | ||
assetPrefix: '/custom-asset-prefix', | ||
async rewrites() { | ||
return { | ||
afterFiles: [ | ||
{ | ||
source: '/custom-asset-prefix/:path*', | ||
destination: '/:path*', | ||
}, | ||
{ | ||
source: '/not-custom-asset-prefix/:path*', | ||
destination: '/:path*', | ||
}, | ||
], | ||
} | ||
}, | ||
} |