Skip to content

Commit db1d87e

Browse files
authored
fix: check if endpoint exists for dynamic pages when checking for red… (#6994)
* fix: check if endpoint exists for dynamic pages when checking for redirect * fix: remove transfer encoding when overriding response
1 parent 1ff368f commit db1d87e

File tree

15 files changed

+938
-3
lines changed

15 files changed

+938
-3
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"test:init:cli-help": "npm run start -- --help",
5151
"test:init:eleventy-deps": "cd tests/integration/__fixtures__/eleventy-site && pnpm install --frozen-lockfile",
5252
"test:init:hugo-deps": "npm ci --prefix tests/integration/__fixtures__/hugo-site --no-audit",
53-
"test:init:next-deps": "npm ci --prefix tests/integration/__fixtures__/next-app-without-config --no-audit",
53+
"test:init:next-deps": "npm ci --prefix tests/integration/__fixtures__/next-app-without-config --no-audit && npm ci --prefix tests/integration/__fixtures__/next-app --no-audit",
5454
"test:dev:vitest": "vitest run tests/unit/ && vitest run tests/integration",
5555
"test:ci:vitest:unit": "vitest run --coverage tests/unit/",
5656
"test:ci:vitest:integration": "vitest run --coverage tests/integration/",

src/utils/proxy.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ const getStatic = async function (pathname: string, publicFolder: string) {
149149
return `/${path.relative(publicFolder, file)}`
150150
}
151151

152+
const isEndpointExists = async function (endpoint: string, origin: string) {
153+
const url = new URL(endpoint, origin)
154+
try {
155+
const res = await fetch(url, { method: 'HEAD' })
156+
return res.status !== 404
157+
} catch (e) {
158+
return false
159+
}
160+
}
161+
152162
// @ts-expect-error TS(7006) FIXME: Parameter 'match' implicitly has an 'any' type.
153163
const isExternal = function (match) {
154164
return match.to && match.to.match(/^https?:\/\//)
@@ -344,8 +354,10 @@ const serveRedirect = async function ({
344354
const reqUrl = reqToURL(req, req.url)
345355

346356
const staticFile = await getStatic(decodeURIComponent(reqUrl.pathname), options.publicFolder)
347-
if (staticFile) {
348-
req.url = encodeURI(staticFile) + reqUrl.search
357+
const endpointExists = !staticFile && (await isEndpointExists(decodeURIComponent(reqUrl.pathname), options.target))
358+
if (staticFile || endpointExists) {
359+
const pathname = staticFile || reqUrl.pathname
360+
req.url = encodeURI(pathname) + reqUrl.search
349361
// if there is an existing static file and it is not a forced redirect, return the file
350362
if (!match.force) {
351363
return proxy.web(req, res, { ...options, staticFile })
@@ -695,6 +707,7 @@ const initializeProxy = async function ({
695707
...proxyResHeaders,
696708
'content-length': String(responseBody.byteLength),
697709
}
710+
delete proxyResHeaders['transfer-encoding']
698711
}
699712

700713
res.writeHead(responseStatus, proxyResHeaders)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
node_modules
5+
/.pnp
6+
.pnp.js
7+
.yarn/install-state.gz
8+
9+
# testing
10+
/coverage
11+
12+
# next.js
13+
/.next/
14+
/out/
15+
16+
# production
17+
/build
18+
19+
# misc
20+
.DS_Store
21+
*.pem
22+
23+
# debug
24+
npm-debug.log*
25+
yarn-debug.log*
26+
yarn-error.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
next-env.d.ts
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
:root {
2+
--max-width: 1100px;
3+
--border-radius: 12px;
4+
--font-mono: ui-monospace, Menlo, Monaco, "Cascadia Mono", "Segoe UI Mono",
5+
"Roboto Mono", "Oxygen Mono", "Ubuntu Monospace", "Source Code Pro",
6+
"Fira Mono", "Droid Sans Mono", "Courier New", monospace;
7+
8+
--foreground-rgb: 0, 0, 0;
9+
--background-start-rgb: 214, 219, 220;
10+
--background-end-rgb: 255, 255, 255;
11+
12+
--primary-glow: conic-gradient(
13+
from 180deg at 50% 50%,
14+
#16abff33 0deg,
15+
#0885ff33 55deg,
16+
#54d6ff33 120deg,
17+
#0071ff33 160deg,
18+
transparent 360deg
19+
);
20+
--secondary-glow: radial-gradient(
21+
rgba(255, 255, 255, 1),
22+
rgba(255, 255, 255, 0)
23+
);
24+
25+
--tile-start-rgb: 239, 245, 249;
26+
--tile-end-rgb: 228, 232, 233;
27+
--tile-border: conic-gradient(
28+
#00000080,
29+
#00000040,
30+
#00000030,
31+
#00000020,
32+
#00000010,
33+
#00000010,
34+
#00000080
35+
);
36+
37+
--callout-rgb: 238, 240, 241;
38+
--callout-border-rgb: 172, 175, 176;
39+
--card-rgb: 180, 185, 188;
40+
--card-border-rgb: 131, 134, 135;
41+
}
42+
43+
@media (prefers-color-scheme: dark) {
44+
:root {
45+
--foreground-rgb: 255, 255, 255;
46+
--background-start-rgb: 0, 0, 0;
47+
--background-end-rgb: 0, 0, 0;
48+
49+
--primary-glow: radial-gradient(rgba(1, 65, 255, 0.4), rgba(1, 65, 255, 0));
50+
--secondary-glow: linear-gradient(
51+
to bottom right,
52+
rgba(1, 65, 255, 0),
53+
rgba(1, 65, 255, 0),
54+
rgba(1, 65, 255, 0.3)
55+
);
56+
57+
--tile-start-rgb: 2, 13, 46;
58+
--tile-end-rgb: 2, 5, 19;
59+
--tile-border: conic-gradient(
60+
#ffffff80,
61+
#ffffff40,
62+
#ffffff30,
63+
#ffffff20,
64+
#ffffff10,
65+
#ffffff10,
66+
#ffffff80
67+
);
68+
69+
--callout-rgb: 20, 20, 20;
70+
--callout-border-rgb: 108, 108, 108;
71+
--card-rgb: 100, 100, 100;
72+
--card-border-rgb: 200, 200, 200;
73+
}
74+
}
75+
76+
* {
77+
box-sizing: border-box;
78+
padding: 0;
79+
margin: 0;
80+
}
81+
82+
html,
83+
body {
84+
max-width: 100vw;
85+
overflow-x: hidden;
86+
}
87+
88+
body {
89+
color: rgb(var(--foreground-rgb));
90+
background: linear-gradient(
91+
to bottom,
92+
transparent,
93+
rgb(var(--background-end-rgb))
94+
)
95+
rgb(var(--background-start-rgb));
96+
}
97+
98+
a {
99+
color: inherit;
100+
text-decoration: none;
101+
}
102+
103+
@media (prefers-color-scheme: dark) {
104+
html {
105+
color-scheme: dark;
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Inter } from 'next/font/google'
2+
import './globals.css'
3+
4+
const inter = Inter({ subsets: ['latin'] })
5+
6+
export const metadata = {
7+
title: 'Create Next App',
8+
description: 'Generated by create next app',
9+
}
10+
11+
export default function RootLayout({ children }) {
12+
return (
13+
<html lang="en">
14+
<body className={inter.className}>{children}</body>
15+
</html>
16+
)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import Image from 'next/image'
2+
import styles from './page.module.css'
3+
4+
export default function Home() {
5+
return (
6+
<main className={styles.main}>
7+
<div className={styles.description}>
8+
<p>
9+
Get started by editing&nbsp;
10+
<code className={styles.code}>app/page.js</code>
11+
</p>
12+
<div>
13+
<a
14+
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
15+
target="_blank"
16+
rel="noopener noreferrer"
17+
>
18+
By{' '}
19+
<Image src="/vercel.svg" alt="Vercel Logo" className={styles.vercelLogo} width={100} height={24} priority />
20+
</a>
21+
</div>
22+
</div>
23+
24+
<div className={styles.center}>
25+
<Image className={styles.logo} src="/next.svg" alt="Next.js Logo" width={180} height={37} priority />
26+
</div>
27+
28+
<div className={styles.grid}>
29+
<a
30+
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
31+
className={styles.card}
32+
target="_blank"
33+
rel="noopener noreferrer"
34+
>
35+
<h2>
36+
Docs <span>-&gt;</span>
37+
</h2>
38+
<p>Find in-depth information about Next.js features and API.</p>
39+
</a>
40+
41+
<a
42+
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
43+
className={styles.card}
44+
target="_blank"
45+
rel="noopener noreferrer"
46+
>
47+
<h2>
48+
Learn <span>-&gt;</span>
49+
</h2>
50+
<p>Learn about Next.js in an interactive course with&nbsp;quizzes!</p>
51+
</a>
52+
53+
<a
54+
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
55+
className={styles.card}
56+
target="_blank"
57+
rel="noopener noreferrer"
58+
>
59+
<h2>
60+
Templates <span>-&gt;</span>
61+
</h2>
62+
<p>Explore starter templates for Next.js.</p>
63+
</a>
64+
65+
<a
66+
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
67+
className={styles.card}
68+
target="_blank"
69+
rel="noopener noreferrer"
70+
>
71+
<h2>
72+
Deploy <span>-&gt;</span>
73+
</h2>
74+
<p>Instantly deploy your Next.js site to a shareable URL with Vercel.</p>
75+
</a>
76+
</div>
77+
</main>
78+
)
79+
}

0 commit comments

Comments
 (0)