我有一个产品页面/products/[slug].js
我对 wordpress/graphql 站点使用增量静态生成:
export async function getStaticProps(context) {
const {params: { slug }} = context
const {data} = await client.query(({
query: PRODUCT_SLUG,
variables: { slug }
}));
return {
props: {
categoryName: data?.productCategory?.name ?? '',
products: data?.productCategory?.products?.nodes ?? []
},
revalidate: 1
}
}
export async function getStaticPaths () {
const { data } = await client.query({
query: PRODUCT_SLUGS,
})
const pathsData = []
data?.productCategories?.nodes && data?.productCategories?.nodes.map((productCategory) => {
if (!isEmpty(productCategory?.slug)) {
pathsData.push({ params: { slug: productCategory?.slug } …Run Code Online (Sandbox Code Playgroud) 我认为这是一个非常普通的问题,但我在谷歌上找不到任何东西。
我正在学习 NextJs(使用 TypeScript)并且我有一个站点成功地使用动态路由、SSR 和增量再生,所有设置和部署到 Vercel。这是我的动态路由处理程序中的GetStaticProps和GetStaticPaths代码示例:
export const getStaticPaths: GetStaticPaths = async () => {
const routes = new CmsHelper().GetRoutes();
const paths = (await routes).items.map((item, index, items) => {
return item.fields.urlPath;
})
return {
paths: paths,
fallback: 'blocking',
};
}
export const getStaticProps: GetStaticProps = async (context) => {
const urlParts = context.params?.url as string[] || [];
const urlPath = `/${urlParts.join('/')}`;
const article = await new CmsHelper().GetArticle(urlPath);
return {
props: {
article
},
revalidate: 10,
} …Run Code Online (Sandbox Code Playgroud) 我的 ISR 有一点问题。我的 revalidate 属性等于 1s,如下所示
export async function getStaticProps({ params }) {
const data = await client.getEntries({
content_type: "product",
"fields.name": params.slug,
});
if (!data.items[0]) {
return {
notFound: true,
};
}
return {
props: {
article: data.items[0],
revalidate: 1,
},
};
}
Run Code Online (Sandbox Code Playgroud)
当我在 Contentful 中创建产品时,页面会按我的预期创建。当我想进入不存在的页面时,我会收到预期的 404 错误。当我更改现有产品中 Contentful 中的某些内容或将其删除时,问题就会出现。
当我在 Contentful 中删除产品时,产品页面中的产品列表会更新并且产品会消失,但我仍然可以进入该产品的页面。此外,当我重命名产品名称时,产品列表也会更新,但我仍然可以访问较早的页面名称。
有什么办法可以解决这个问题吗?
获取静态路径
export async function getStaticPaths() {
const data = await client.getEntries({
content_type: "product",
});
return {
paths: data.items.map((item) => ({
params: { slug: item.fields.name },
})), …Run Code Online (Sandbox Code Playgroud)