From 4365131bcbfc91241c2fee1e08f1e9b0256cb9fe Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 8 Nov 2021 21:43:23 +0000 Subject: [PATCH 1/3] Don't proxy middleware if host is the same --- packages/next/server/next-server.ts | 59 ++++++++++++++++++----------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 1d4d37651ed14..6e3098f434e6a 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -248,8 +248,8 @@ export default class Server { : null, optimizeImages: !!this.nextConfig.experimental.optimizeImages, optimizeCss: this.nextConfig.experimental.optimizeCss, - disableOptimizedLoading: - this.nextConfig.experimental.disableOptimizedLoading, + disableOptimizedLoading: this.nextConfig.experimental + .disableOptimizedLoading, domainLocales: this.nextConfig.i18n?.domains, distDir: this.distDir, concurrentFeatures: this.nextConfig.experimental.concurrentFeatures, @@ -379,8 +379,9 @@ export default class Server { typeof req.headers['x-matched-path'] === 'string' ) { const reqUrlIsDataUrl = req.url?.includes('/_next/data') - const matchedPathIsDataUrl = - req.headers['x-matched-path']?.includes('/_next/data') + const matchedPathIsDataUrl = req.headers['x-matched-path']?.includes( + '/_next/data' + ) const isDataUrl = reqUrlIsDataUrl || matchedPathIsDataUrl let parsedPath = parseUrl( @@ -882,8 +883,9 @@ export default class Server { finished: true, } } - const { imageOptimizer } = - require('./image-optimizer') as typeof import('./image-optimizer') + const { + imageOptimizer, + } = require('./image-optimizer') as typeof import('./image-optimizer') return imageOptimizer( server, @@ -1052,8 +1054,9 @@ export default class Server { let updatedDestination = formatUrl(parsedDestination) if (updatedDestination.startsWith('/')) { - updatedDestination = - normalizeRepeatedSlashes(updatedDestination) + updatedDestination = normalizeRepeatedSlashes( + updatedDestination + ) } res.setHeader('Location', updatedDestination) @@ -1224,7 +1227,12 @@ export default class Server { if (result.response.headers.has('x-middleware-rewrite')) { const rewrite = result.response.headers.get('x-middleware-rewrite')! const rewriteParsed = simpleParseUrl(rewrite) - if (rewriteParsed.protocol) { + if ( + rewriteParsed.protocol && + (rewriteParsed.port + ? `${rewriteParsed.hostname}:${rewriteParsed.port}` + : rewriteParsed.hostname) !== req.headers.host + ) { return proxyRequest(req, res, rewriteParsed) } @@ -1746,7 +1754,9 @@ export default class Server { return null } - protected async getStaticPaths(pathname: string): Promise<{ + protected async getStaticPaths( + pathname: string + ): Promise<{ staticPaths: string[] | undefined fallbackMode: 'static' | 'blocking' | false }> { @@ -1755,8 +1765,8 @@ export default class Server { const staticPaths = undefined // Read whether or not fallback should exist from the manifest. - const fallbackField = - this.getPrerenderManifest().dynamicRoutes[pathname].fallback + const fallbackField = this.getPrerenderManifest().dynamicRoutes[pathname] + .fallback return { staticPaths, @@ -1952,17 +1962,20 @@ export default class Server { // handle serverless if (isLikeServerless) { - const renderResult = await ( - components.ComponentMod as any - ).renderReqToHTML(req, res, 'passthrough', { - locale, - locales, - defaultLocale, - optimizeCss: this.renderOpts.optimizeCss, - distDir: this.distDir, - fontManifest: this.renderOpts.fontManifest, - domainLocales: this.renderOpts.domainLocales, - }) + const renderResult = await (components.ComponentMod as any).renderReqToHTML( + req, + res, + 'passthrough', + { + locale, + locales, + defaultLocale, + optimizeCss: this.renderOpts.optimizeCss, + distDir: this.distDir, + fontManifest: this.renderOpts.fontManifest, + domainLocales: this.renderOpts.domainLocales, + } + ) body = renderResult.html pageData = renderResult.renderOpts.pageData From af2262196ea1a8f326f5e0b187bd124a900a777b Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 8 Nov 2021 22:32:20 +0000 Subject: [PATCH 2/3] Add integration test --- test/integration/middleware/core/test/index.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/integration/middleware/core/test/index.test.js b/test/integration/middleware/core/test/index.test.js index 2c386dfe99709..067e21bb4efa9 100644 --- a/test/integration/middleware/core/test/index.test.js +++ b/test/integration/middleware/core/test/index.test.js @@ -156,6 +156,16 @@ function rewriteTests(locale = '') { expect($('.title').text()).toBe('About Page') }) + it(`${locale} should rewrite when not using localhost`, async () => { + const res = await fetchViaHTTP( + `http://localtest.me:${context.appPort}`, + `${locale}/rewrites/rewrite-me-without-hard-navigation` + ) + const html = await res.text() + const $ = cheerio.load(html) + expect($('.title').text()).toBe('About Page') + }) + it(`${locale} should rewrite to Vercel`, async () => { const res = await fetchViaHTTP( context.appPort, From c4e0ce0c1294147037239cedf8e02cf18595fded Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 9 Nov 2021 10:20:44 +0000 Subject: [PATCH 3/3] lint --- packages/next/server/next-server.ts | 52 ++++++++++++----------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 6e3098f434e6a..e406b9e501da5 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -248,8 +248,8 @@ export default class Server { : null, optimizeImages: !!this.nextConfig.experimental.optimizeImages, optimizeCss: this.nextConfig.experimental.optimizeCss, - disableOptimizedLoading: this.nextConfig.experimental - .disableOptimizedLoading, + disableOptimizedLoading: + this.nextConfig.experimental.disableOptimizedLoading, domainLocales: this.nextConfig.i18n?.domains, distDir: this.distDir, concurrentFeatures: this.nextConfig.experimental.concurrentFeatures, @@ -379,9 +379,8 @@ export default class Server { typeof req.headers['x-matched-path'] === 'string' ) { const reqUrlIsDataUrl = req.url?.includes('/_next/data') - const matchedPathIsDataUrl = req.headers['x-matched-path']?.includes( - '/_next/data' - ) + const matchedPathIsDataUrl = + req.headers['x-matched-path']?.includes('/_next/data') const isDataUrl = reqUrlIsDataUrl || matchedPathIsDataUrl let parsedPath = parseUrl( @@ -883,9 +882,8 @@ export default class Server { finished: true, } } - const { - imageOptimizer, - } = require('./image-optimizer') as typeof import('./image-optimizer') + const { imageOptimizer } = + require('./image-optimizer') as typeof import('./image-optimizer') return imageOptimizer( server, @@ -1054,9 +1052,8 @@ export default class Server { let updatedDestination = formatUrl(parsedDestination) if (updatedDestination.startsWith('/')) { - updatedDestination = normalizeRepeatedSlashes( - updatedDestination - ) + updatedDestination = + normalizeRepeatedSlashes(updatedDestination) } res.setHeader('Location', updatedDestination) @@ -1754,9 +1751,7 @@ export default class Server { return null } - protected async getStaticPaths( - pathname: string - ): Promise<{ + protected async getStaticPaths(pathname: string): Promise<{ staticPaths: string[] | undefined fallbackMode: 'static' | 'blocking' | false }> { @@ -1765,8 +1760,8 @@ export default class Server { const staticPaths = undefined // Read whether or not fallback should exist from the manifest. - const fallbackField = this.getPrerenderManifest().dynamicRoutes[pathname] - .fallback + const fallbackField = + this.getPrerenderManifest().dynamicRoutes[pathname].fallback return { staticPaths, @@ -1962,20 +1957,17 @@ export default class Server { // handle serverless if (isLikeServerless) { - const renderResult = await (components.ComponentMod as any).renderReqToHTML( - req, - res, - 'passthrough', - { - locale, - locales, - defaultLocale, - optimizeCss: this.renderOpts.optimizeCss, - distDir: this.distDir, - fontManifest: this.renderOpts.fontManifest, - domainLocales: this.renderOpts.domainLocales, - } - ) + const renderResult = await ( + components.ComponentMod as any + ).renderReqToHTML(req, res, 'passthrough', { + locale, + locales, + defaultLocale, + optimizeCss: this.renderOpts.optimizeCss, + distDir: this.distDir, + fontManifest: this.renderOpts.fontManifest, + domainLocales: this.renderOpts.domainLocales, + }) body = renderResult.html pageData = renderResult.renderOpts.pageData