-
Notifications
You must be signed in to change notification settings - Fork 29.5k
rfc: deprecate middleware
and recommend proxy
#84119
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
Open
devjiwonchoi
wants to merge
17
commits into
canary
Choose a base branch
from
jiwon/09-23-rename_middleware_to_proxy_
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+741
−36
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3f234cc
Rename `middleware` to `proxy`
devjiwonchoi 946457d
test: add proxy
devjiwonchoi 11c9414
test: add proxy with middleware
devjiwonchoi ababb36
test: warn check
devjiwonchoi ba34c51
test cliOutput from first
devjiwonchoi 553729e
add warn to dev
devjiwonchoi a9fd80b
test: try add retry
devjiwonchoi 4c951e2
test: use redirect and matcher
devjiwonchoi 5a1931f
test: remove proxy test
devjiwonchoi c476839
fix: adapter should have proxy
devjiwonchoi 2aca72a
fix: isProxy can be /src/proxy
devjiwonchoi dff1667
test: app-middleware-proxy
devjiwonchoi 9290d16
update turbopack
devjiwonchoi ea55e92
turbopack: update entries
devjiwonchoi d7fd3e7
fix: userland_path does not start with "/"
devjiwonchoi b48fdbd
add to exclude of experimenta tests manifest
devjiwonchoi 7b0414a
turbopack: remove unnecessary
devjiwonchoi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -948,7 +948,12 @@ export async function createEntrypoints( | |||||
isDev: false, | ||||||
}) | ||||||
} else if (isMiddlewareFile(page)) { | ||||||
server[serverBundlePath.replace('src/', '')] = getEdgeServerEntry({ | ||||||
server[ | ||||||
serverBundlePath | ||||||
// proxy.js still uses middleware.js for bundle path | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
.replace('proxy', 'middleware') | ||||||
.replace('src/', '') | ||||||
] = getEdgeServerEntry({ | ||||||
...params, | ||||||
rootDir, | ||||||
absolutePagePath: absolutePagePath, | ||||||
|
@@ -1023,20 +1028,21 @@ export async function createEntrypoints( | |||||
: undefined, | ||||||
}).import | ||||||
} | ||||||
edgeServer[serverBundlePath] = getEdgeServerEntry({ | ||||||
...params, | ||||||
rootDir, | ||||||
absolutePagePath: absolutePagePath, | ||||||
bundlePath: clientBundlePath, | ||||||
isDev: false, | ||||||
isServerComponent, | ||||||
page, | ||||||
middleware: staticInfo?.middleware, | ||||||
pagesType, | ||||||
appDirLoader, | ||||||
preferredRegion: staticInfo.preferredRegion, | ||||||
middlewareConfig: staticInfo.middleware, | ||||||
}) | ||||||
edgeServer[serverBundlePath.replace('proxy', 'middleware')] = | ||||||
getEdgeServerEntry({ | ||||||
...params, | ||||||
rootDir, | ||||||
absolutePagePath: absolutePagePath, | ||||||
bundlePath: clientBundlePath, | ||||||
isDev: false, | ||||||
isServerComponent, | ||||||
page, | ||||||
middleware: staticInfo?.middleware, | ||||||
pagesType, | ||||||
appDirLoader, | ||||||
preferredRegion: staticInfo.preferredRegion, | ||||||
middlewareConfig: staticInfo.middleware, | ||||||
}) | ||||||
} | ||||||
}, | ||||||
}) | ||||||
|
This file contains hidden or 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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -21,6 +21,7 @@ import { | |||||||
STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR, | ||||||||
PUBLIC_DIR_MIDDLEWARE_CONFLICT, | ||||||||
MIDDLEWARE_FILENAME, | ||||||||
PROXY_FILENAME, | ||||||||
PAGES_DIR_ALIAS, | ||||||||
INSTRUMENTATION_HOOK_FILENAME, | ||||||||
RSC_PREFETCH_SUFFIX, | ||||||||
|
@@ -1160,6 +1161,10 @@ export default async function build( | |||||||
`^${MIDDLEWARE_FILENAME}\\.(?:${config.pageExtensions.join('|')})$` | ||||||||
) | ||||||||
|
||||||||
const proxyDetectionRegExp = new RegExp( | ||||||||
`^${PROXY_FILENAME}\\.(?:${config.pageExtensions.join('|')})$` | ||||||||
) | ||||||||
|
||||||||
const instrumentationHookDetectionRegExp = new RegExp( | ||||||||
`^${INSTRUMENTATION_HOOK_FILENAME}\\.(?:${config.pageExtensions.join( | ||||||||
'|' | ||||||||
|
@@ -1169,6 +1174,7 @@ export default async function build( | |||||||
const rootDir = path.join((pagesDir || appDir)!, '..') | ||||||||
const includes = [ | ||||||||
middlewareDetectionRegExp, | ||||||||
proxyDetectionRegExp, | ||||||||
instrumentationHookDetectionRegExp, | ||||||||
] | ||||||||
|
||||||||
|
@@ -1183,6 +1189,17 @@ export default async function build( | |||||||
const hasMiddlewareFile = rootPaths.some((p) => | ||||||||
p.includes(MIDDLEWARE_FILENAME) | ||||||||
) | ||||||||
const hasProxyFile = rootPaths.some((p) => p.includes(PROXY_FILENAME)) | ||||||||
if (hasMiddlewareFile) { | ||||||||
if (hasProxyFile) { | ||||||||
throw new Error( | ||||||||
`Both "${MIDDLEWARE_FILENAME}" and "${PROXY_FILENAME}" files are detected. Please use "${PROXY_FILENAME}" instead.` | ||||||||
) | ||||||||
} | ||||||||
Log.warn( | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
`The "${MIDDLEWARE_FILENAME}" file convention is deprecated. Please use "${PROXY_FILENAME}" instead.` | ||||||||
) | ||||||||
} | ||||||||
|
||||||||
NextBuildContext.hasInstrumentationHook = hasInstrumentationHook | ||||||||
|
||||||||
|
@@ -2567,8 +2584,8 @@ export default async function build( | |||||||
) | ||||||||
} | ||||||||
|
||||||||
const middlewareFile = rootPaths.find((p) => | ||||||||
p.includes(MIDDLEWARE_FILENAME) | ||||||||
const middlewareFile = rootPaths.find( | ||||||||
(p) => p.includes(MIDDLEWARE_FILENAME) || p.includes(PROXY_FILENAME) | ||||||||
) | ||||||||
let hasNodeMiddleware = false | ||||||||
|
||||||||
|
@@ -3943,7 +3960,7 @@ export default async function build( | |||||||
rewritesWithHasCount: combinedRewrites.filter((r: any) => !!r.has) | ||||||||
.length, | ||||||||
redirectsWithHasCount: redirects.filter((r: any) => !!r.has).length, | ||||||||
middlewareCount: hasMiddlewareFile ? 1 : 0, | ||||||||
middlewareCount: hasMiddlewareFile || hasProxyFile ? 1 : 0, | ||||||||
totalAppPagesCount, | ||||||||
staticAppPagesCount, | ||||||||
serverAppPagesCount, | ||||||||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
34 changes: 34 additions & 0 deletions
34
test/e2e/app-dir/app-middleware-proxy/app-middleware-proxy-in-src-dir.test.ts
This file contains hidden or 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,34 @@ | ||
/* eslint-env jest */ | ||
import path from 'path' | ||
import { nextTestSetup, FileRef } from 'e2e-utils' | ||
|
||
describe('app dir - with proxy in src dir', () => { | ||
const { next } = nextTestSetup({ | ||
files: { | ||
'src/app': new FileRef(path.join(__dirname, 'app')), | ||
'next.config.js': new FileRef(path.join(__dirname, 'next.config.js')), | ||
'src/proxy.js': ` | ||
import { NextResponse } from 'next/server' | ||
import { cookies } from 'next/headers' | ||
export async function proxy(request) { | ||
const cookie = (await cookies()).get('test-cookie') | ||
return NextResponse.json({ cookie }) | ||
} | ||
`, | ||
}, | ||
}) | ||
|
||
it('works without crashing when using RequestStore', async () => { | ||
const browser = await next.browser('/') | ||
await browser.addCookie({ | ||
name: 'test-cookie', | ||
value: 'test-cookie-response', | ||
}) | ||
await browser.refresh() | ||
|
||
const html = await browser.eval('document.documentElement.innerHTML') | ||
|
||
expect(html).toContain('test-cookie-response') | ||
}) | ||
}) |
30 changes: 30 additions & 0 deletions
30
test/e2e/app-dir/app-middleware-proxy/app-middleware-proxy-without-pages-dir.test.ts
This file contains hidden or 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,30 @@ | ||
/* eslint-env jest */ | ||
import path from 'path' | ||
import { nextTestSetup, FileRef } from 'e2e-utils' | ||
|
||
describe('app dir - proxy without pages dir', () => { | ||
const { next } = nextTestSetup({ | ||
files: { | ||
app: new FileRef(path.join(__dirname, 'app')), | ||
'next.config.js': new FileRef(path.join(__dirname, 'next.config.js')), | ||
'proxy.js': ` | ||
import { NextResponse } from 'next/server' | ||
export async function proxy(request) { | ||
return new NextResponse('redirected') | ||
} | ||
export const config = { | ||
matcher: '/headers' | ||
} | ||
`, | ||
}, | ||
}) | ||
|
||
// eslint-disable-next-line jest/no-identical-title | ||
it('Updates headers', async () => { | ||
const html = await next.render('/headers') | ||
|
||
expect(html).toContain('redirected') | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use
FileSystemPath::file_stem
: https://turbopack-rust-docs.vercel.sh/rustdoc/turbo_tasks_fs/struct.FileSystemPath.html#method.file_stem