Skip to content

Commit

Permalink
Support trailingSlash in middleware SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Feb 18, 2022
1 parent 4ad1c5a commit fa83f73
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion 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
2 changes: 2 additions & 0 deletions packages/next/shared/lib/router/utils/parse-next-url.ts
Expand Up @@ -5,6 +5,7 @@ import type { NextConfig, DomainLocale } from '../../../../server/config-shared'
import type { ParsedUrl } from './parse-url'
import type { PathLocale } from '../../i18n/normalize-locale-path'
import { hasBasePath, replaceBasePath } from '../../../../server/router'
import { removePathTrailingSlash } from '../../../../client/normalize-trailing-slash'

interface Params {
headers?: { [key: string]: string | string[] | undefined }
Expand Down Expand Up @@ -38,6 +39,7 @@ export function parseNextUrl({ headers, nextConfig, url = '/' }: Params) {
}
}

urlParsed.pathname = removePathTrailingSlash(urlParsed.pathname)
return urlParsed
}

Expand Down
45 changes: 40 additions & 5 deletions test/production/react-18-streaming-ssr/index.test.ts
@@ -1,8 +1,9 @@
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'
import webdriver from 'next-webdriver'

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 +28,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 +40,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)
})
})

0 comments on commit fa83f73

Please sign in to comment.