Skip to content

Commit

Permalink
Use static rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxachun committed Nov 29, 2024
1 parent 05d1baf commit 7dcb93a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions site/src/app/[domain]/[language]/[[...path]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const dynamic = "error";

import { gql, previewParams } from "@comet/cms-site";
import { documentTypes } from "@src/documents";
import { GQLPageTreeNodeScopeInput } from "@src/graphql.generated";
Expand Down
2 changes: 2 additions & 0 deletions site/src/app/[domain]/[language]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SitePreviewProvider } from "@comet/cms-site";
import { IntlProvider } from "@src/util/IntlProvider";
import { loadMessages } from "@src/util/loadMessages";
import { setNotFoundContext } from "@src/util/NotFoundContext";
import { getSiteConfigForDomain } from "@src/util/siteConfig";
import { SiteConfigProvider } from "@src/util/SiteConfigProvider";
import { draftMode } from "next/headers";
Expand All @@ -11,6 +12,7 @@ export default async function Page({ children, params: { domain, language } }: P
if (!siteConfig.scope.languages.includes(language)) {
language = "en";
}
setNotFoundContext({ domain, language });

const isDraftModeEnabled = draftMode().isEnabled;

Expand Down
9 changes: 6 additions & 3 deletions site/src/app/[domain]/[language]/not-found.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { getNotFoundContext } from "@src/util/NotFoundContext";
import Link from "next/link";

export default async function NotFound404(): Promise<JSX.Element> {
const scope = getNotFoundContext() || { domain: "main", language: "en" };

return (
<html>
<html lang={scope.language}>
<body>
<p>Page not found.</p>
<Link href="/">Return Home</Link>
<p>Page not found (Scope {JSON.stringify(scope)}).</p>
<Link href={`/${scope.language}`}>Return Home</Link>
</body>
</html>
);
Expand Down
25 changes: 25 additions & 0 deletions site/src/util/NotFoundContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ContentScope } from "@src/site-configs";
import { cache } from "react";

// https://github.com/vercel/next.js/discussions/43179#discussioncomment-11192893
/**
* This is a workaround until Next.js adds support for true Server Context
* It works by using React `cache` to store the value for the lifetime of one rendering.
* Meaning it's available to all server components down the tree after it's set.
* Do not use this unless necessary.
*
* @warning This is a temporary workaround.
*/
function createServerContext<T>(defaultValue: T): [() => T, (v: T) => void] {
const getRef = cache(() => ({ current: defaultValue }));

const getValue = (): T => getRef().current;

const setValue = (value: T) => {
getRef().current = value;
};

return [getValue, setValue];
}

export const [getNotFoundContext, setNotFoundContext] = createServerContext<ContentScope | null>(null);

0 comments on commit 7dcb93a

Please sign in to comment.