-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle
getStaticPaths
with prerendered pages (#5734)
* fix(#5661): ensure getStaticPaths is correctly handled for prerendered pages * test: add prerender getStaticPaths cases * chore: add changeset * test: add props to test suite * chore: update lockfile Co-authored-by: Nate Moore <nate@astro.build>
- Loading branch information
1 parent
cc4606d
commit 55cea0a
Showing
18 changed files
with
408 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fix `prerender` when used with `getStaticPaths` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/ssr-prerender-get-static-paths/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/ssr-prerender-get-static-paths", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...s/astro/test/fixtures/ssr-prerender-get-static-paths/src/pages/[...calledTwiceTest].astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
export function getStaticPaths({ paginate }) { | ||
if (globalThis.isCalledOnce) { | ||
throw new Error("Can only be called once!"); | ||
} | ||
globalThis.isCalledOnce = true; | ||
return [ | ||
{params: {calledTwiceTest: 'a'}}, | ||
{params: {calledTwiceTest: 'b'}}, | ||
{params: {calledTwiceTest: 'c'}}, | ||
]; | ||
} | ||
export const prerender = true; | ||
const { params } = Astro; | ||
--- | ||
|
||
<html> | ||
<head> | ||
<title>Page {params.calledTwiceTest}</title> | ||
</head> | ||
<body></body> | ||
</html> |
19 changes: 19 additions & 0 deletions
19
...ges/astro/test/fixtures/ssr-prerender-get-static-paths/src/pages/blog/[year]/[slug].astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
export async function getStaticPaths() { | ||
return [ | ||
{ params: { year: '2022', slug: 'post-1' } }, | ||
{ params: { year: 2022, slug: 'post-2' } }, | ||
{ params: { slug: 'post-2', year: '2022' } }, | ||
] | ||
} | ||
export const prerender = true; | ||
const { year, slug } = Astro.params | ||
--- | ||
|
||
<html> | ||
<head> | ||
<title>{year} | {slug}</title> | ||
</head> | ||
<body></body> | ||
</html> |
16 changes: 16 additions & 0 deletions
16
packages/astro/test/fixtures/ssr-prerender-get-static-paths/src/pages/data/[slug].json.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const prerender = true; | ||
|
||
export async function getStaticPaths() { | ||
return [ | ||
{ params: { slug: 'thing1' } }, | ||
{ params: { slug: 'thing2' } } | ||
]; | ||
} | ||
|
||
export async function get() { | ||
return { | ||
body: JSON.stringify({ | ||
title: '[slug]' | ||
}, null, 4) | ||
}; | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/astro/test/fixtures/ssr-prerender-get-static-paths/src/pages/food/[name].astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
export async function getStaticPaths() { | ||
return [ | ||
{ | ||
params: { name: 'tacos' }, | ||
props: { yum: 10 }, | ||
}, | ||
{ | ||
params: { name: 'potatoes' }, | ||
props: { yum: 7 }, | ||
}, | ||
{ | ||
params: { name: 'spaghetti' }, | ||
props: { yum: 5 }, | ||
} | ||
] | ||
} | ||
export const prerender = true; | ||
const { yum } = Astro.props; | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>Food</title> | ||
</head> | ||
<body> | ||
<p id="url">{ Astro.url.pathname }</p> | ||
<p id="props">{ yum }</p> | ||
</body> | ||
</html> |
10 changes: 10 additions & 0 deletions
10
...s/astro/test/fixtures/ssr-prerender-get-static-paths/src/pages/nested-arrays/[slug].astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
export function getStaticPaths() { | ||
return [ | ||
[ { params: {slug: "slug1"} } ], | ||
[ { params: {slug: "slug2"} } ], | ||
] | ||
} | ||
export const prerender = true; | ||
--- |
23 changes: 23 additions & 0 deletions
23
packages/astro/test/fixtures/ssr-prerender-get-static-paths/src/pages/pizza/[...pizza].astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
export function getStaticPaths() { | ||
return [{ | ||
params: { pizza: 'papa-johns' }, | ||
}, { | ||
params: { pizza: 'dominos' }, | ||
}, { | ||
params: { pizza: 'grimaldis/new-york' }, | ||
}] | ||
} | ||
export const prerender = true; | ||
const { pizza } = Astro.params | ||
--- | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>{pizza ?? 'The landing page'}</title> | ||
</head> | ||
<body> | ||
<h1>Welcome to {pizza ?? 'The landing page'}</h1> | ||
</body> | ||
</html> |
22 changes: 22 additions & 0 deletions
22
...tro/test/fixtures/ssr-prerender-get-static-paths/src/pages/pizza/[cheese]-[topping].astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
export function getStaticPaths() { | ||
return [{ | ||
params: { cheese: 'mozzarella', topping: 'pepperoni' }, | ||
}, { | ||
params: { cheese: 'provolone', topping: 'sausage' }, | ||
}] | ||
} | ||
export const prerender = true; | ||
const { cheese, topping } = Astro.params | ||
--- | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>{cheese}</title> | ||
</head> | ||
<body> | ||
<h1>🍕 It's pizza time</h1> | ||
<p>{cheese}-{topping}</p> | ||
</body> | ||
</html> |
30 changes: 30 additions & 0 deletions
30
packages/astro/test/fixtures/ssr-prerender-get-static-paths/src/pages/posts/[page].astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
export async function getStaticPaths() { | ||
return [ | ||
{ | ||
params: { page: 1 }, | ||
}, | ||
{ | ||
params: { page: 2 }, | ||
}, | ||
{ | ||
params: { page: 3 } | ||
} | ||
] | ||
}; | ||
export const prerender = true; | ||
const { page } = Astro.params | ||
const canonicalURL = new URL(Astro.url.pathname, Astro.site); | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>Posts Page {page}</title> | ||
<link rel="canonical" href={canonicalURL.href}> | ||
</head> | ||
<body> | ||
<h1>Welcome to page {page}</h1> | ||
</body> | ||
</html> |
Oops, something went wrong.