Skip to content

Commit

Permalink
Ensure basePath is not applied for external GSSP redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Apr 4, 2021
1 parent 9d2962c commit 0821b57
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,11 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
}
const statusCode = getRedirectStatus(redirect)

if (basePath && redirect.basePath !== false) {
if (
basePath &&
redirect.basePath !== false &&
redirect.destination.startsWith('/')
) {
redirect.destination = `${basePath}${redirect.destination}`
}

Expand Down
6 changes: 5 additions & 1 deletion packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,11 @@ export default class Server {
const statusCode = getRedirectStatus(redirect)
const { basePath } = this.nextConfig

if (basePath && redirect.basePath !== false) {
if (
basePath &&
redirect.basePath !== false &&
redirect.destination.startsWith('/')
) {
redirect.destination = `${basePath}${redirect.destination}`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export const getStaticProps = ({ params }) => {
if (params.post.startsWith('redir')) {
let destination = '/404'

if (params.post.includes('dest-')) {
if (params.post.includes('dest-external')) {
destination = 'https://example.com'
} else if (params.post.includes('dest-')) {
destination = params.post.split('dest-').pop().replace(/_/g, '/')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const getServerSideProps = ({ params }) => {
if (params.post.startsWith('redir')) {
let destination = '/404'

if (params.post.includes('dest-')) {
if (params.post.includes('dest-external')) {
destination = 'https://example.com'
} else if (params.post.includes('dest-')) {
destination = params.post.split('dest-').pop().replace(/_/g, '/')
}

Expand Down
48 changes: 48 additions & 0 deletions test/integration/gssp-redirect-base-path/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,54 @@ const runTests = (isDev) => {
expect(pathname).toBe('/missing')
})

it('should apply redirect when fallback GSP page is visited directly (external domain)', async () => {
const browser = await webdriver(
appPort,
`${basePath}/gsp-blog/redirect-dest-external`,
true,
true
)

await check(
() => browser.eval(() => document.location.hostname),
'example.com'
)

const initialHref = await browser.eval(() => window.initialHref)
expect(initialHref).toBe(null)
})

it('should apply redirect when fallback GSSP page is visited directly (external domain)', async () => {
const browser = await webdriver(
appPort,
`${basePath}/gssp-blog/redirect-dest-external`,
true,
true
)

await check(
() => browser.eval(() => document.location.hostname),
'example.com'
)

const initialHref = await browser.eval(() => window.initialHref)
expect(initialHref).toBe(null)

const res = await fetchViaHTTP(
appPort,
`${basePath}/gssp-blog/redirect-dest-external`,
undefined,
{
redirect: 'manual',
}
)
expect(res.status).toBe(307)

const parsed = url.parse(res.headers.get('location'))
expect(parsed.hostname).toBe('example.com')
expect(parsed.pathname).toBe('/')
})

it('should apply redirect when GSSP page is navigated to client-side (internal dynamic)', async () => {
const browser = await webdriver(
appPort,
Expand Down
4 changes: 3 additions & 1 deletion test/integration/gssp-redirect/pages/gsp-blog/[post].js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export const getStaticProps = ({ params }) => {
if (params.post.startsWith('redir')) {
let destination = '/404'

if (params.post.includes('dest-')) {
if (params.post.includes('dest-external')) {
destination = 'https://example.com'
} else if (params.post.includes('dest-')) {
destination = params.post.split('dest-').pop().replace(/_/g, '/')
}

Expand Down
4 changes: 3 additions & 1 deletion test/integration/gssp-redirect/pages/gssp-blog/[post].js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const getServerSideProps = ({ params }) => {
if (params.post.startsWith('redir')) {
let destination = '/404'

if (params.post.includes('dest-')) {
if (params.post.includes('dest-external')) {
destination = 'https://example.com'
} else if (params.post.includes('dest-')) {
destination = params.post.split('dest-').pop().replace(/_/g, '/')
}

Expand Down
48 changes: 48 additions & 0 deletions test/integration/gssp-redirect/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,54 @@ const runTests = (isDev) => {
expect(pathname).toBe('/missing')
})

it('should apply redirect when fallback GSP page is visited directly (external domain)', async () => {
const browser = await webdriver(
appPort,
'/gsp-blog/redirect-dest-external',
true,
true
)

await check(
() => browser.eval(() => document.location.hostname),
'example.com'
)

const initialHref = await browser.eval(() => window.initialHref)
expect(initialHref).toBe(null)
})

it('should apply redirect when fallback GSSP page is visited directly (external domain)', async () => {
const browser = await webdriver(
appPort,
'/gssp-blog/redirect-dest-external',
true,
true
)

await check(
() => browser.eval(() => document.location.hostname),
'example.com'
)

const initialHref = await browser.eval(() => window.initialHref)
expect(initialHref).toBe(null)

const res = await fetchViaHTTP(
appPort,
'/gssp-blog/redirect-dest-external',
undefined,
{
redirect: 'manual',
}
)
expect(res.status).toBe(307)

const parsed = url.parse(res.headers.get('location'))
expect(parsed.hostname).toBe('example.com')
expect(parsed.pathname).toBe('/')
})

it('should apply redirect when GSSP page is navigated to client-side (internal dynamic)', async () => {
const browser = await webdriver(
appPort,
Expand Down

0 comments on commit 0821b57

Please sign in to comment.