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

Improve error message for endpoint getStaticPaths with undefined value #6353

Merged
merged 70 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
51f8f19
fix · it can access website without index in dev environment and prod…
JerryWu1234 Feb 24, 2023
b1e081b
pass test
JerryWu1234 Feb 24, 2023
76f7502
Merge branch 'main' into fix_route
JerryWu1234 Feb 24, 2023
4693f74
Merge branch 'main' into fix_route
JerryWu1234 Feb 26, 2023
1063d67
Merge branch 'main' into fix_route
JerryWu1234 Feb 28, 2023
d155a24
add test
JerryWu1234 Feb 28, 2023
bd2c39c
delete redundent code
JerryWu1234 Feb 28, 2023
832925a
delete blank
JerryWu1234 Feb 28, 2023
e218027
better detect file
JerryWu1234 Feb 28, 2023
de2fe25
fix warn message
JerryWu1234 Mar 1, 2023
5bd09e9
add test about endpoints
JerryWu1234 Mar 1, 2023
fe77a42
Merge branch 'main' into fix_route
JerryWu1234 Mar 1, 2023
eed40e4
Merge branch 'main' into fix_route
JerryWu1234 Mar 1, 2023
b9b943e
code come back
JerryWu1234 Mar 2, 2023
df9e457
Merge branch 'main' into fix_route
JerryWu1234 Mar 2, 2023
4c9e5df
throw an error with endpoints
JerryWu1234 Mar 2, 2023
850aee9
fix wouldnt create file when miss json
JerryWu1234 Mar 2, 2023
893dc1e
fix focused on ts
JerryWu1234 Mar 2, 2023
61f408a
After testing, the *.ts files under the endpoint cannot be ignored
JerryWu1234 Mar 2, 2023
f6452f1
fix object bug
JerryWu1234 Mar 2, 2023
07581f5
Merge branch 'main' into fix_route
JerryWu1234 Mar 3, 2023
80b952b
Merge branch 'main' into fix_route
JerryWu1234 Mar 3, 2023
d2ee4bb
add special a test
JerryWu1234 Mar 3, 2023
c342a14
add special a test
JerryWu1234 Mar 3, 2023
f99a6fc
optimize tips
JerryWu1234 Mar 3, 2023
f4ce639
fix error test
JerryWu1234 Mar 3, 2023
dbf7d5f
Merge branch 'main' into fix_route
JerryWu1234 Mar 5, 2023
5a548ad
Merge branch 'main' into fix_route
JerryWu1234 Mar 6, 2023
71c2ecf
Merge branch 'main' into fix_route
JerryWu1234 Mar 7, 2023
0d6522f
tweak the condition in core file and delete test
JerryWu1234 Mar 7, 2023
72b20a9
add test
JerryWu1234 Mar 7, 2023
e376d4f
Merge branch 'main' into fix_route
JerryWu1234 Mar 7, 2023
190d937
fix bug and test unit
JerryWu1234 Mar 7, 2023
bae3955
fix: done all featrue about getstaticPath
JerryWu1234 Mar 7, 2023
9df9064
Merge branch 'main' into fix_route
JerryWu1234 Mar 7, 2023
566a5bc
fix: test bug
JerryWu1234 Mar 7, 2023
d083491
Improve endpoint static check
bluwy Mar 7, 2023
06b481f
Update error message
bluwy Mar 7, 2023
c41c8db
Rename test file
bluwy Mar 7, 2023
87d5d09
Update lockfile
bluwy Mar 7, 2023
5d7a518
Fix test
bluwy Mar 7, 2023
d5a8ad0
fix eslint error
JerryWu1234 Mar 8, 2023
88aea64
solve collision
Mar 8, 2023
c6d82dd
fix:The name of the code was incorrect and reducing judgement conditi…
JerryWu1234 Mar 8, 2023
394f246
Merge branch 'main' into pr/wulinsheng123/6353
bluwy Mar 8, 2023
c85192e
Fix param check
bluwy Mar 8, 2023
6e89c71
Fix test
bluwy Mar 8, 2023
bf21d16
Merge branch 'main' into fix_route
JerryWu1234 Mar 9, 2023
8d6c9ff
fix changeset tips
JerryWu1234 Mar 9, 2023
73152d7
Merge branch 'main' into fix_route
JerryWu1234 Mar 9, 2023
a6dd8a3
Merge branch 'main' into fix_route
JerryWu1234 Mar 9, 2023
658e6d2
Merge branch 'main' into fix_route
JerryWu1234 Mar 10, 2023
f85eaba
beautify the description of the error
JerryWu1234 Mar 10, 2023
e5dddbc
Merge branch 'main' into fix_route
JerryWu1234 Mar 10, 2023
5718b55
Merge branch 'main' into fix_route
JerryWu1234 Mar 12, 2023
ad129e0
Merge branch 'main' into fix_route
JerryWu1234 Mar 14, 2023
e4321ee
Merge branch 'main' into fix_route
JerryWu1234 Mar 14, 2023
9c77f55
optimize the format of error
Mar 15, 2023
2498269
Merge branch 'main' into fix_route
JerryWu1234 Mar 15, 2023
05add29
Merge branch 'main' into fix_route
JerryWu1234 Mar 16, 2023
0659a25
Update changeset and docs
bluwy Mar 16, 2023
8291973
Merge branch 'main' into fix_route
bluwy Mar 16, 2023
185d594
Merge branch 'main' into fix_route
JerryWu1234 Mar 17, 2023
8aff1ee
Merge branch 'main' into fix_route
JerryWu1234 Mar 20, 2023
a78a870
Merge branch 'main' into fix_route
JerryWu1234 Mar 20, 2023
3db077b
Merge branch 'main' into fix_route
JerryWu1234 Mar 21, 2023
2f0c105
Merge branch 'main' into fix_route
JerryWu1234 Mar 21, 2023
7d86b33
Merge branch 'main' into fix_route
JerryWu1234 Mar 21, 2023
467e242
Merge branch 'main' into fix_route
JerryWu1234 Mar 28, 2023
56d3473
fix comment
Mar 28, 2023
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
5 changes: 5 additions & 0 deletions .changeset/small-knives-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Throw better error when a dynamic endpoint without additional extensions is prerendered with `undefined` params.
21 changes: 21 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,27 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati
)} are supported for optimization.`,
hint: "If you do not need optimization, using an `img` tag directly instead of the `Image` component might be what you're looking for.",
},
/**
* @docs
* @see
* - [`getStaticPaths()`](https://docs.astro.build/en/reference/api-reference/#getstaticpaths)
* - [`params`](https://docs.astro.build/en/reference/api-reference/#params)
* @description
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
* The endpoint is prerendered with an `undefined` param but the generated file and folder names will collide.
*
* An additional extension can be added to the endpoint file name to generate the file with a different name. For example, renaming `pages/api/[slug].ts` to `pages/api/[slug].json.ts`.
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
*/
PrerenderDynamicEndpointPathCollide: {
title: 'Prerendered dynamic endpoint has path collision.',
code: 3026,
message: (pathname: string) =>
`Could not render \`${pathname}\` with an \`undefined\` param as the generated path will collide during prerendering. ` +
`Prevent passing \`undefined\` as \`params\` for the endpoint's \`getStaticPaths()\` function, ` +
`or add an additional extension to the endpoint's filename.`,
hint: (filename: string) =>
`Rename \`${filename}\` to \`${filename.replace(/\.(js|ts)/, (m) => `.json` + m)}\``,
},

// No headings here, that way Vite errors are merged with Astro ones in the docs, which makes more sense to users.
// Vite Errors - 4xxx
/**
Expand Down
21 changes: 21 additions & 0 deletions packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ export async function getParamsAndProps(
const paramsMatch = route.pattern.exec(pathname);
if (paramsMatch) {
params = getParams(route.params)(paramsMatch);

// If we have an endpoint at `src/pages/api/[slug].ts` that's prerendered, and the `slug`
// is `undefined`, throw an error as we can't generate the `/api` file and `/api` directory
// at the same time. Using something like `[slug].json.ts` instead will work.
if (route.type === 'endpoint' && mod.getStaticPaths) {
const lastSegment = route.segments[route.segments.length - 1];
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
const paramValues = Object.values(params);
const lastParam = paramValues[paramValues.length - 1];
// Check last segment is solely `[slug]` or `[...slug]` case (dynamic). Make sure it's not
// `foo[slug].js` by checking segment length === 1. Also check here if that param is undefined.
if (lastSegment.length === 1 && lastSegment[0].dynamic && lastParam === undefined) {
throw new AstroError({
...AstroErrorData.PrerenderDynamicEndpointPathCollide,
message: AstroErrorData.PrerenderDynamicEndpointPathCollide.message(route.route),
hint: AstroErrorData.PrerenderDynamicEndpointPathCollide.hint(route.component),
location: {
file: route.component,
},
});
}
}
}
}
let routeCacheEntry = routeCache.get(route);
Expand Down
52 changes: 52 additions & 0 deletions packages/astro/test/dynamic-endpoint-collision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Dynamic endpoint collision', () => {
describe('build', () => {
let fixture;
let errorMsg;
before(async () => {
fixture = await loadFixture({
root: './fixtures/dynamic-endpoint-collision/',
});
try {
await fixture.build();
} catch (error) {
errorMsg = error;
}
});

it('throw error when dynamic endpoint has path collision', async () => {
expect(errorMsg.errorCode).to.eq(3026);
});
});

describe('dev', () => {
let fixture;
let devServer;

before(async () => {
fixture = await loadFixture({
root: './fixtures/dynamic-endpoint-collision/',
});

devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});

it('throw error when dynamic endpoint has path collision', async () => {
const html = await fixture.fetch('/api/catch').then((res) => res.text());
const $ = cheerioLoad(html);
expect($('title').text()).to.equal('PrerenderDynamicEndpointPathCollide');
});

it("don't throw error when dynamic endpoint doesn't load the colliding path", async () => {
const res = await fixture.fetch('/api/catch/one').then((r) => r.text());
expect(res).to.equal('{"slug":"one"}');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';


// https://astro.build/config
export default defineConfig({

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/dynamic-endpoint-collision",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { APIRoute } from "astro";

const slugs = ["one", undefined];

export const get: APIRoute = ({ params }) => {
return {
body: JSON.stringify({
slug: params.slug || "index",
}),
};
};

export function getStaticPaths() {
return slugs.map((u) => ({ params: { slug: u } }));
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export interface Props {
href: string,
}
const {href, title, body} = Astro.props;
debugger;
---
<li class="link-card">
<a href={href}>
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.