Skip to content

Commit

Permalink
fix: turn off revalidation for redirects and 404 (#1465)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Sep 12, 2024
1 parent 21be2e5 commit 876df1d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/fdr-sdk/src/navigation/types/NavigationNodePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export function isPage(node: NavigationNode): node is NavigationNodePage {
return (
isApiLeaf(node) ||
node.type === "changelog" ||
node.type === "changelogYear" ||
node.type === "changelogMonth" ||
// TODO: Uncomment when changelog years and months are visitable
// node.type === "changelogYear" ||
// node.type === "changelogMonth" ||
hasMarkdown(node)
);
}
4 changes: 2 additions & 2 deletions packages/ui/docs-bundle/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line import/no-internal-modules
import { FernUser, getAuthEdgeConfig, verifyFernJWTConfig } from "@fern-ui/ui/auth";
import { NextRequest, NextResponse, type MiddlewareConfig, type NextMiddleware } from "next/server";
import { NextRequest, NextResponse, type NextMiddleware } from "next/server";
import urlJoin from "url-join";
import { extractBuildId, extractNextDataPathname } from "./utils/extractNextDataPathname";
import { getPageRoute, getPageRouteMatch, getPageRoutePath } from "./utils/pageRoutes";
Expand Down Expand Up @@ -134,7 +134,7 @@ export const middleware: NextMiddleware = async (request) => {
return NextResponse.rewrite(nextUrl, { request: { headers } });
};

export const config: MiddlewareConfig = {
export const config = {
matcher: [
/**
* Match all requests to posthog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const handler: NextApiHandler = async (

const node = FernNavigation.utils.convertLoadDocsForUrlResponse(docs);
const slugCollector = NodeCollector.collect(node);
const urls = slugCollector.getSlugs().map((slug) => urljoin(xFernHost, slug));
const urls = slugCollector.getPageSlugs().map((slug) => urljoin(xFernHost, slug));

const results = await Promise.all(
urls.map(async (url): Promise<RevalidatePathResult> => {
Expand Down
14 changes: 9 additions & 5 deletions packages/ui/docs-bundle/src/utils/getDocsPageProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function getDocsPageProps(
slug: string[],
): Promise<GetStaticDocsPagePropsResult> {
if (xFernHost == null || Array.isArray(xFernHost)) {
return { notFound: true };
return { notFound: true, revalidate: true };
}

const pathname = decodeURI(slug != null ? slug.join("/") : "");
Expand All @@ -71,9 +71,9 @@ export async function getDocsPageProps(
console.log(`[getDocsPageProps] Fetch completed in ${end - start}ms for ${url}`);
if (!docs.ok) {
if ((docs.error as any).content.statusCode === 401) {
return { redirect: await getUnauthenticatedRedirect(xFernHost, `/${slug.join("/")}`) };
return { redirect: await getUnauthenticatedRedirect(xFernHost, `/${slug.join("/")}`), revalidate: true };
} else if ((docs.error as any).content.statusCode === 404) {
return { notFound: true };
return { notFound: true, revalidate: true };
}

// eslint-disable-next-line no-console
Expand Down Expand Up @@ -191,6 +191,7 @@ async function convertDocsToDocsPageProps({
destination: conformTrailingSlash(redirect.destination),
permanent: redirect.permanent ?? false,
},
revalidate: true,
};
}

Expand All @@ -208,6 +209,7 @@ async function convertDocsToDocsPageProps({
destination: encodeURI(urljoin("/", root.slug)),
permanent: false,
},
revalidate: true,
};
}

Expand All @@ -229,10 +231,11 @@ async function convertDocsToDocsPageProps({
destination: encodeURI(urljoin("/", node.redirect) || "/"),
permanent: false,
},
revalidate: true,
};
}

return { notFound: true };
return { notFound: true, revalidate: true };
}

if (node.type === "redirect") {
Expand All @@ -241,6 +244,7 @@ async function convertDocsToDocsPageProps({
destination: encodeURI(urljoin("/", node.redirect)),
permanent: false,
},
revalidate: true,
};
}

Expand All @@ -260,7 +264,7 @@ async function convertDocsToDocsPageProps({
if (content == null) {
// eslint-disable-next-line no-console
console.error(`Failed to resolve path for ${url}`);
return { notFound: true };
return { notFound: true, revalidate: true };
}

const colors = {
Expand Down

0 comments on commit 876df1d

Please sign in to comment.