Skip to content

Commit 7343a85

Browse files
committedSep 1, 2023
fix: external redirects (fix #1103)
1 parent 99f5ba9 commit 7343a85

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default Page
2+
3+
import React from 'react'
4+
5+
function Page() {
6+
return <p>I will never be shown because of throw redirect() in the guard() hook.</p>
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default guard
2+
3+
import { redirect } from 'vite-plugin-ssr/abort'
4+
5+
async function guard() {
6+
throw redirect('https://vite-plugin-ssr.com')
7+
}

‎test/abort-v1/renderer/PageLayout.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function PageLayout({ children, pageContext }: { children: React.ReactNode; page
1717
<Nav href="/render-homepage" />
1818
<Nav href="/show-error-page" />
1919
<Nav href="/permanent-redirect" />
20+
<Nav href="/redirect-external" />
2021
</Sidebar>
2122
<Content>{children}</Content>
2223
</Layout>

‎test/abort-v1/test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
export { testRun as test }
22

33
import { run, page, test, expect, getServerUrl, fetchHtml, autoRetry, expectLog, partRegex } from '@brillout/test-e2e'
4-
import { ensureWasClientSideRouted, expectUrl, hydrationDone, testCounter, expectPageContextJsonRequest } from '../utils'
4+
import {
5+
ensureWasClientSideRouted,
6+
expectUrl,
7+
hydrationDone,
8+
testCounter,
9+
expectPageContextJsonRequest
10+
} from '../utils'
511

612
function testRun(cmd: string, pageContextInitHasClientData = false) {
713
run(cmd)
@@ -108,4 +114,10 @@ function testRun(cmd: string, pageContextInitHasClientData = false) {
108114
await page.click('a[href="/permanent-redirect"]')
109115
expectUrl('/')
110116
})
117+
118+
test('external redirect', async () => {
119+
await page.goto(getServerUrl() + '/')
120+
await page.click('a[href="/redirect-external"]')
121+
await page.waitForURL('https://vite-plugin-ssr.com')
122+
})
111123
}

‎vite-plugin-ssr/node/runtime/renderPage/createHttpResponseObject/assertNoInfiniteHttpRedirect.ts

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ const globalObject = getGlobalObject<{ redirectGraph: Graph }>('assertNoInfinite
99
})
1010

1111
function assertNoInfiniteHttpRedirect(urlRedirectOriginal: string, urlRedirectPathnameLogical: string) {
12+
if (urlRedirectOriginal.startsWith('http')) {
13+
// We assume that the redirect points to an external origin, and we can therefore assume that the app doesn't define an infinite loop (in itself).
14+
// - There isn't a reliable way to check whether the redirect points to an external origin or the same origin: the user usually passes the URL without origin.
15+
// ```js
16+
// // URL origin is usually missing
17+
// renderPage({ urlOriginal: '/some/pathname' })
18+
// ```
19+
return
20+
}
1221
assert(urlRedirectOriginal.startsWith('/'))
1322
assert(urlRedirectPathnameLogical.startsWith('/'))
1423
const graph = copy(globalObject.redirectGraph)

0 commit comments

Comments
 (0)
Please sign in to comment.