Unverified Commit 6ed281df authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Support trailingSlash in middleware SSR (#34544)

## Bug

Fixes #34527 

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
parent a4f5463e
...@@ -8,6 +8,7 @@ import type { ParsedNextUrl } from '../shared/lib/router/utils/parse-next-url' ...@@ -8,6 +8,7 @@ import type { ParsedNextUrl } from '../shared/lib/router/utils/parse-next-url'
import type { PrerenderManifest } from '../build' import type { PrerenderManifest } from '../build'
import type { Rewrite } from '../lib/load-custom-routes' import type { Rewrite } from '../lib/load-custom-routes'
import type { BaseNextRequest, BaseNextResponse } from './base-http' import type { BaseNextRequest, BaseNextResponse } from './base-http'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { execOnce } from '../shared/lib/utils' import { execOnce } from '../shared/lib/utils'
import { import {
...@@ -34,7 +35,6 @@ import { ...@@ -34,7 +35,6 @@ import {
CLIENT_PUBLIC_FILES_PATH, CLIENT_PUBLIC_FILES_PATH,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync' import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
import { format as formatUrl, UrlWithParsedQuery } from 'url' import { format as formatUrl, UrlWithParsedQuery } from 'url'
import compression from 'next/dist/compiled/compression' import compression from 'next/dist/compiled/compression'
...@@ -73,6 +73,7 @@ import { loadEnvConfig } from '@next/env' ...@@ -73,6 +73,7 @@ import { loadEnvConfig } from '@next/env'
import { getCustomRoute } from './server-route-utils' import { getCustomRoute } from './server-route-utils'
import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring' import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring'
import ResponseCache from '../server/response-cache' import ResponseCache from '../server/response-cache'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
import { clonableBodyForRequest } from './body-streams' import { clonableBodyForRequest } from './body-streams'
export * from './base-server' export * from './base-server'
...@@ -1030,7 +1031,8 @@ export default class NextNodeServer extends BaseServer { ...@@ -1030,7 +1031,8 @@ export default class NextNodeServer extends BaseServer {
}, },
}) })
if (!this.middleware?.some((m) => m.match(parsedUrl.pathname))) { const normalizedPathname = removePathTrailingSlash(parsedUrl.pathname)
if (!this.middleware?.some((m) => m.match(normalizedPathname))) {
return { finished: false } return { finished: false }
} }
...@@ -1212,6 +1214,9 @@ export default class NextNodeServer extends BaseServer { ...@@ -1212,6 +1214,9 @@ export default class NextNodeServer extends BaseServer {
onWarning?: (warning: Error) => void onWarning?: (warning: Error) => void
}): Promise<FetchEventResult | null> { }): Promise<FetchEventResult | null> {
this.middlewareBetaWarning() this.middlewareBetaWarning()
const normalizedPathname = removePathTrailingSlash(
params.parsedUrl.pathname
)
// For middleware to "fetch" we must always provide an absolute URL // For middleware to "fetch" we must always provide an absolute URL
const url = getRequestMeta(params.request, '__NEXT_INIT_URL')! const url = getRequestMeta(params.request, '__NEXT_INIT_URL')!
...@@ -1222,11 +1227,11 @@ export default class NextNodeServer extends BaseServer { ...@@ -1222,11 +1227,11 @@ export default class NextNodeServer extends BaseServer {
} }
const page: { name?: string; params?: { [key: string]: string } } = {} const page: { name?: string; params?: { [key: string]: string } } = {}
if (await this.hasPage(params.parsedUrl.pathname)) { if (await this.hasPage(normalizedPathname)) {
page.name = params.parsedUrl.pathname page.name = params.parsedUrl.pathname
} else if (this.dynamicRoutes) { } else if (this.dynamicRoutes) {
for (const dynamicRoute of this.dynamicRoutes) { for (const dynamicRoute of this.dynamicRoutes) {
const matchParams = dynamicRoute.match(params.parsedUrl.pathname) const matchParams = dynamicRoute.match(normalizedPathname)
if (matchParams) { if (matchParams) {
page.name = dynamicRoute.page page.name = dynamicRoute.page
page.params = matchParams page.params = matchParams
...@@ -1244,7 +1249,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1244,7 +1249,7 @@ export default class NextNodeServer extends BaseServer {
: undefined : undefined
for (const middleware of this.middleware || []) { for (const middleware of this.middleware || []) {
if (middleware.match(params.parsedUrl.pathname)) { if (middleware.match(normalizedPathname)) {
if (!(await this.hasMiddleware(middleware.page, middleware.ssr))) { if (!(await this.hasMiddleware(middleware.page, middleware.ssr))) {
console.warn(`The Edge Function for ${middleware.page} was not found`) console.warn(`The Edge Function for ${middleware.page} was not found`)
continue continue
......
import { createNext } from 'e2e-utils' import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils' import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
describe('react-18-streaming-ssr in minimal mode', () => { describe('react 18 streaming SSR in minimal mode', () => {
let next: NextInstance let next: NextInstance
beforeAll(async () => { beforeAll(async () => {
...@@ -27,10 +27,7 @@ describe('react-18-streaming-ssr in minimal mode', () => { ...@@ -27,10 +27,7 @@ describe('react-18-streaming-ssr in minimal mode', () => {
react: '18.0.0-rc.0', react: '18.0.0-rc.0',
'react-dom': '18.0.0-rc.0', 'react-dom': '18.0.0-rc.0',
}, },
skipStart: true,
}) })
await next.start()
}) })
afterAll(() => { afterAll(() => {
delete process.env.NEXT_PRIVATE_MINIMAL_MODE delete process.env.NEXT_PRIVATE_MINIMAL_MODE
...@@ -42,3 +39,40 @@ describe('react-18-streaming-ssr in minimal mode', () => { ...@@ -42,3 +39,40 @@ describe('react-18-streaming-ssr in minimal mode', () => {
expect(html).toContain('static streaming') expect(html).toContain('static streaming')
}) })
}) })
describe('react 18 streaming SSR with custom next configs', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/hello.js': `
export default function Page() {
return <p>hello</p>
}
`,
},
nextConfig: {
trailingSlash: true,
experimental: {
reactRoot: true,
runtime: 'edge',
},
},
dependencies: {
react: '18.0.0-rc.0',
'react-dom': '18.0.0-rc.0',
},
})
})
afterAll(() => next.destroy())
it('should redirect paths without trailing-slash and render when slash is appended', async () => {
const page = '/hello'
const html = await renderViaHTTP(next.url, page + '/')
const res = await fetchViaHTTP(next.url, page, {}, { redirect: 'manual' })
expect(html).toContain('hello')
expect(res.status).toBe(308)
})
})
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