with
- Astro v 4.8.1
using static generator option in Astro,i.e. output: "static"
default defineConfig({
site: "https://abc.xyz",
prefetch: true,
output: "static"
});
having a tag page on routing oftag/
using getStaticPaths()
method
use
src/pages/tag/[tag].astro
---
const { tag } = Astro.params;
export const prerender = false;
export function getStaticPaths() {
return [
{ params: { tag: "alpha" } },
{ params: { tag: "beta" } }
];
}
---
<article title={tag || "default" }>
<span >no tags found</span>
</title>
here tag/alpha
and tag/beta
works but user type tag/gamma
then it reaches to 404 page
desired behavior is not to show 404 but custom message , although url does not change to /404
and in terminal it throw Warning
A
getStaticPaths()
route pattern was matched, but no matching static path was found for requested path/tag/gamma
.
trial 1 adding undefined path
added undefined
route
export function getStaticPaths() {
return [
{ params: { tag: "alpha" } },
{ params: { tag: "beta" } },
{ params: { tag: undefined } }
];
}
but did not work and gives type error missing parameter “tag” as below
trial 2 using a flag
---
const validTag = ["alpha", "beta" ];
export function getStaticPaths() {
return [
{ params: { tag: "alpha" } },
{ params: { tag: "beta" } }
];
}
const { tag } = Astro.params;
const isValidTag = validTag.includes(tag);
---
<article title={tag || "default" }>
{isValidTag ? <p>Valid tag</p> : <p>No tag found</p>}
</article>
this also not working; show 404 page.
1