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

Update experimental skipTrailingSlashRedirect handling #43201

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ export function getDefineEnv({
'process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE': JSON.stringify(
config.experimental.skipMiddlewareUrlNormalize
),
'process.env.__NEXT_MANUAL_TRAILING_SLASH': JSON.stringify(
config.experimental?.skipTrailingSlashRedirect
),
'process.env.__NEXT_HAS_WEB_VITALS_ATTRIBUTION': JSON.stringify(
config.experimental.webVitalsAttribution &&
config.experimental.webVitalsAttribution.length > 0
Expand Down
2 changes: 1 addition & 1 deletion packages/next/client/normalize-trailing-slash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { parsePath } from '../shared/lib/router/utils/parse-path'
* in `next.config.js`.
*/
export const normalizePathTrailingSlash = (path: string) => {
if (!path.startsWith('/')) {
if (!path.startsWith('/') || process.env.__NEXT_MANUAL_TRAILING_SLASH) {
return path
}

Expand Down
4 changes: 4 additions & 0 deletions test/e2e/skip-trailing-slash-redirect/app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export default function Page(props) {
to another
</Link>
<br />
<Link href="/another/" id="to-another-with-slash">
to another
</Link>
<br />
<Link href="/blog/first" id="to-blog-first">
to /blog/first
</Link>
Expand Down
48 changes: 48 additions & 0 deletions test/e2e/skip-trailing-slash-redirect/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,54 @@ describe('skip-trailing-slash-redirect', () => {
expect(await res.text()).toContain('another page')
})

it('should not apply trailing slash to links on client', async () => {
const browser = await webdriver(next.url, '/')
await browser.eval('window.beforeNav = 1')

expect(
new URL(
await browser.elementByCss('#to-another').getAttribute('href'),
'http://n'
).pathname
).toBe('/another')

await browser.elementByCss('#to-another').click()
await browser.waitForElementByCss('#another')

expect(await browser.eval('window.location.pathname')).toBe('/another')

await browser.back().waitForElementByCss('#to-another')

expect(
new URL(
await browser
.elementByCss('#to-another-with-slash')
.getAttribute('href'),
'http://n'
).pathname
).toBe('/another/')

await browser.elementByCss('#to-another-with-slash').click()
await browser.waitForElementByCss('#another')

expect(await browser.eval('window.location.pathname')).toBe('/another/')

await browser.back().waitForElementByCss('#to-another')
expect(await browser.eval('window.beforeNav')).toBe(1)
})

it('should not apply trailing slash on load on client', async () => {
let browser = await webdriver(next.url, '/another')
await check(() => browser.eval('next.router.isReady ? "yes": "no"'), 'yes')

expect(await browser.eval('location.pathname')).toBe('/another')

browser = await webdriver(next.url, '/another/')
await check(() => browser.eval('next.router.isReady ? "yes": "no"'), 'yes')

expect(await browser.eval('location.pathname')).toBe('/another/')
})

it('should respond to index correctly', async () => {
const res = await fetchViaHTTP(next.url, '/', undefined, {
redirect: 'manual',
Expand Down