Skip to content
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

Support trailingSlash in middleware SSR #34544

Merged
merged 3 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/next/server/next-server.ts
Expand Up @@ -8,6 +8,7 @@ import type { ParsedNextUrl } from '../shared/lib/router/utils/parse-next-url'
import type { PrerenderManifest } from '../build'
import type { Rewrite } from '../lib/load-custom-routes'
import type { BaseNextRequest, BaseNextResponse } from './base-http'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'

import { execOnce } from '../shared/lib/utils'
import {
Expand All @@ -34,7 +35,6 @@ import {
CLIENT_PUBLIC_FILES_PATH,
SERVERLESS_DIRECTORY,
} from '../shared/lib/constants'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
import { format as formatUrl, UrlWithParsedQuery } from 'url'
import compression from 'next/dist/compiled/compression'
Expand Down Expand Up @@ -73,6 +73,7 @@ import { loadEnvConfig } from '@next/env'
import { getCustomRoute } from './server-route-utils'
import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring'
import ResponseCache from '../server/response-cache'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
import { clonableBodyForRequest } from './body-streams'

export * from './base-server'
Expand Down Expand Up @@ -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 }
}

Expand Down Expand Up @@ -1212,6 +1214,9 @@ export default class NextNodeServer extends BaseServer {
onWarning?: (warning: Error) => void
}): Promise<FetchEventResult | null> {
this.middlewareBetaWarning()
const normalizedPathname = removePathTrailingSlash(
params.parsedUrl.pathname
)

// For middleware to "fetch" we must always provide an absolute URL
const url = getRequestMeta(params.request, '__NEXT_INIT_URL')!
Expand All @@ -1222,11 +1227,11 @@ export default class NextNodeServer extends BaseServer {
}

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
} else if (this.dynamicRoutes) {
for (const dynamicRoute of this.dynamicRoutes) {
const matchParams = dynamicRoute.match(params.parsedUrl.pathname)
const matchParams = dynamicRoute.match(normalizedPathname)
if (matchParams) {
page.name = dynamicRoute.page
page.params = matchParams
Expand All @@ -1244,7 +1249,7 @@ export default class NextNodeServer extends BaseServer {
: undefined

for (const middleware of this.middleware || []) {
if (middleware.match(params.parsedUrl.pathname)) {
if (middleware.match(normalizedPathname)) {
if (!(await this.hasMiddleware(middleware.page, middleware.ssr))) {
console.warn(`The Edge Function for ${middleware.page} was not found`)
continue
Expand Down
44 changes: 39 additions & 5 deletions test/production/react-18-streaming-ssr/index.test.ts
@@ -1,8 +1,8 @@
import { createNext } from 'e2e-utils'
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

beforeAll(async () => {
Expand All @@ -27,10 +27,7 @@ describe('react-18-streaming-ssr in minimal mode', () => {
react: '18.0.0-rc.0',
'react-dom': '18.0.0-rc.0',
},
skipStart: true,
})

await next.start()
})
afterAll(() => {
delete process.env.NEXT_PRIVATE_MINIMAL_MODE
Expand All @@ -42,3 +39,40 @@ describe('react-18-streaming-ssr in minimal mode', () => {
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)
})
})