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

fix: use a client-side navigation when redirecting to a rewriten URL #25990

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
12fb755
fix: client-side navigation when redirecting to a rewriten URL
Jun 10, 2021
066846c
refactor: move gssp redirect with rewrites tests next to gssp-redirec…
Jun 13, 2021
84a2234
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
antoinechalifour Jun 13, 2021
fa14159
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
antoinechalifour Jun 14, 2021
658b9d6
revert: component name in documentation updated while refactoring
Jun 14, 2021
6120fd3
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
antoinechalifour Jun 15, 2021
94c2fa8
reove check known route as there is a fallback anyway
Jun 15, 2021
b1e8c77
update build output size
Jun 15, 2021
31c46a0
Update test/integration/gssp-redirect-with-rewrites/test/index.test.js
antoinechalifour Jun 15, 2021
682935b
Update test/integration/gssp-redirect-with-rewrites/test/index.test.js
antoinechalifour Jun 15, 2021
9a85b56
Update test/integration/gssp-redirect-with-rewrites/test/index.test.js
antoinechalifour Jun 15, 2021
9544294
fix typo
Jun 15, 2021
8823e06
add test for redirects to unknown route
Jun 15, 2021
9a862d3
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
antoinechalifour Jun 16, 2021
a3f6a61
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
ijjk Jun 16, 2021
fd722c4
update size test
ijjk Jun 16, 2021
efa698b
Merge remote-tracking branch 'upstream/canary' into redirect-getserve…
ijjk Jun 16, 2021
ab5954d
Update test
ijjk Jun 16, 2021
f932956
lint-fix
ijjk Jun 16, 2021
1f46641
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
ijjk Jun 16, 2021
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
7 changes: 6 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,12 @@ export default class Router implements BaseRouter {
pages
)

if (pages.includes(parsedHref.pathname)) {
const isDestinationAKnownPage = pages.includes(parsedHref.pathname)
const isDestinationARewrittenUrl = rewrites.afterFiles.some(
(x: any) => x.source === parsedHref.pathname
)

if (isDestinationAKnownPage || isDestinationARewrittenUrl) {
antoinechalifour marked this conversation as resolved.
Show resolved Hide resolved
const { url: newUrl, as: newAs } = prepareUrlAs(
this,
destination,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
async rewrites() {
return [
{
source: '/',
destination: '/some-page',
},
]
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function SomeOtherPage() {
return (
<main>
<h1>Hello world</h1>
</main>
)
}

export const getServerSideProps = ({ query }) => {
return {
redirect: {
destination: `${query.redirect}?message=${query.message}`,
permanent: false,
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Link from 'next/link'

export default function SomePage({ message }) {
return (
<main>
<h1>Hello {message}</h1>
<Link href="/some-other-page?redirect=/&message=refreshed">
<a id="link-with-rewritten-url" className={message}>
Link with rewritten target url
</a>
</Link>
<Link href="/some-other-page?redirect=/some-page">
<a>Link with client side navigation</a>
</Link>
</main>
)
}

export const getServerSideProps = ({ query }) => ({
props: { message: query.message || 'World ' },
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-env jest */

import { join } from 'path'
import {
renderViaHTTP,
findPort,
launchApp,
killApp,
waitFor,
} from 'next-test-utils'
import webdriver from 'next-webdriver'

// test suites

const context = {}
jest.setTimeout(1000 * 60 * 5)

describe('getServerSideProps redirects', () => {
beforeAll(async () => {
context.appPort = await findPort()
context.server = await launchApp(join(__dirname, '../'), context.appPort, {
env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
})

// pre-build all pages at the start
await Promise.all([
renderViaHTTP(context.appPort, '/'),
renderViaHTTP(context.appPort, '/some-page'),
])
})
afterAll(() => killApp(context.server))

it('should use a client-side navigation for a rewritten URL', async () => {
const browser = await webdriver(context.appPort, '/')

await browser.executeScript(function () {
// During a browser navigation global variables are reset,
// So by chaking that the __SAME_PAGE variable is still defined
// then the client-side navigation has happened
window.__SAME_PAGE = true
})

await browser.elementByCss('#link-with-rewritten-url').click()

// Wait for a potential page reload
antoinechalifour marked this conversation as resolved.
Show resolved Hide resolved
await waitFor(1000)

// Wait until the new props are rendered
await browser.elementByCss('.refreshed')

const isSamePage = await browser.executeScript(function () {
return window.__SAME_PAGE || false
})

expect(isSamePage).toBe(true)
antoinechalifour marked this conversation as resolved.
Show resolved Hide resolved
})
})