-
Notifications
You must be signed in to change notification settings - Fork 2
✨ Move locale into a [locale] route segment so tenant pages render static/ISR #737
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
Open
joshbermanssw
wants to merge
8
commits into
main
Choose a base branch
from
locale-route-segment-735
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7aef0fc
Add @app alias, localeFromBreadcrumbs and resolveRequestRoute helpers…
joshbermanssw de29fac
Address review: dot-boundary domain match, clarify internalPath, null…
joshbermanssw ec3443b
Move route tree under app/[locale]/[product] and rewrite middleware i…
joshbermanssw ded7b33
Thread locale from route params through pages, generateStaticParams, …
joshbermanssw ad09037
Fix page-data API callers for new locale arg; rethrow in blog metadata
joshbermanssw 1138e10
Remove getLocale()/x-language now that locale comes from route params
joshbermanssw a8bd9e6
Raise getPageData fetch revalidate 10->3600 so [filename] ISR takes e…
joshbermanssw 04ba4f7
Raise [filename] generateMetadata fetch revalidate 10->3600 (uncaps r…
joshbermanssw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { localeFromBreadcrumbs } from "@utils/localeFromBreadcrumbs"; | ||
|
|
||
| describe("localeFromBreadcrumbs", () => { | ||
| it("returns 'en' for a top-level file", () => { | ||
| expect(localeFromBreadcrumbs(["YakShaver", "home"])).toBe("en"); | ||
| }); | ||
| it("returns 'zh' when second segment is zh", () => { | ||
| expect(localeFromBreadcrumbs(["YakShaver", "zh", "home"])).toBe("zh"); | ||
| }); | ||
| it("returns 'en' for other products", () => { | ||
| expect(localeFromBreadcrumbs(["SSW", "about"])).toBe("en"); | ||
| }); | ||
| it("handles empty/short arrays", () => { | ||
| expect(localeFromBreadcrumbs([])).toBe("en"); | ||
| expect(localeFromBreadcrumbs(["YakShaver"])).toBe("en"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { resolveRequestRoute } from "@utils/resolveRequestRoute"; | ||
|
|
||
| const productList = [ | ||
| { product: "YakShaver", domain: "yakshaver.ai" }, | ||
| { product: "SSW", domain: "ssw.com.au" }, | ||
| ]; | ||
| const env = { defaultProduct: "YakShaver" }; | ||
|
|
||
| describe("resolveRequestRoute", () => { | ||
| it("production en domain → /en/<product>/path", () => { | ||
| expect( | ||
| resolveRequestRoute({ hostname: "ssw.com.au", pathname: "/about", productList, env }) | ||
| ).toEqual({ locale: "en", product: "SSW", internalPath: "/en/SSW/about" }); | ||
| }); | ||
| it("production Chinese domain → /zh/YakShaver/path", () => { | ||
| expect( | ||
| resolveRequestRoute({ hostname: "yakshaver.cn", pathname: "/features", productList, env }) | ||
| ).toEqual({ locale: "zh", product: "YakShaver", internalPath: "/zh/YakShaver/features" }); | ||
| }); | ||
| it("production www Chinese domain → zh", () => { | ||
| const zhResult = resolveRequestRoute({ hostname: "www.yakshaver.com.cn", pathname: "/", productList, env }); | ||
| expect(zhResult).not.toBeNull(); | ||
| expect(zhResult?.locale).toBe("zh"); | ||
| }); | ||
| it("root path on en domain → /en/<product>", () => { | ||
| expect( | ||
| resolveRequestRoute({ hostname: "yakshaver.ai", pathname: "/", productList, env }) | ||
| ).toEqual({ locale: "en", product: "YakShaver", internalPath: "/en/YakShaver" }); | ||
| }); | ||
| it("local default product, en", () => { | ||
| expect( | ||
| resolveRequestRoute({ hostname: "localhost:3000", pathname: "/blog", productList, env }) | ||
| ).toEqual({ locale: "en", product: "YakShaver", internalPath: "/en/YakShaver/blog" }); | ||
| }); | ||
| it("local /zh prefix → zh, prefix stripped", () => { | ||
| expect( | ||
| resolveRequestRoute({ hostname: "localhost:3000", pathname: "/zh/blog", productList, env }) | ||
| ).toEqual({ locale: "zh", product: "YakShaver", internalPath: "/zh/YakShaver/blog" }); | ||
| }); | ||
| it("local path starting with a product is not double-prefixed", () => { | ||
| expect( | ||
| resolveRequestRoute({ hostname: "localhost:3000", pathname: "/SSW/about", productList, env }) | ||
| ).toEqual({ locale: "en", product: "SSW", internalPath: "/en/SSW/about" }); | ||
| }); | ||
| it("staging (vercel.app) behaves like local", () => { | ||
| const stagingResult = resolveRequestRoute({ hostname: "ssw-products.vercel.app", pathname: "/", productList, env }); | ||
| expect(stagingResult).not.toBeNull(); | ||
| expect(stagingResult?.product).toBe("YakShaver"); | ||
| }); | ||
| it("unknown production host → null", () => { | ||
| expect( | ||
| resolveRequestRoute({ hostname: "unknown.example.com", pathname: "/", productList, env }) | ||
| ).toBeNull(); | ||
| }); | ||
| }); |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import HomePageClient from "@comps/shared/HomePageClient"; | ||
| import client from "@tina/__generated__/client"; | ||
| import { setPageMetadata } from "@utils/setPageMetaData"; | ||
| import { getPageWithFallback } from "@utils/i18n"; | ||
| import getPageData from "@utils/pages/getPageData"; | ||
| import NotFoundError from "@/errors/not-found"; | ||
| import ClientFallbackPage from "@app/client-fallback-page"; | ||
| import { notFound } from "next/navigation"; | ||
| import { localeFromBreadcrumbs } from "@utils/localeFromBreadcrumbs"; | ||
|
|
||
| export const dynamic = "force-static"; | ||
|
|
||
| interface FilePageProps { | ||
| params: Promise<{ locale: string; product: string; filename: string }>; | ||
| } | ||
|
|
||
| export async function generateMetadata({ params }: FilePageProps) { | ||
| const { locale, product, filename } = await params; | ||
| try { | ||
| const fileData = await getPageWithFallback({ product, filename, locale, revalidate: 3600, branch: "main" }); | ||
| return setPageMetadata(fileData.data?.pages?.seo, product); | ||
| } catch (error) { | ||
| if (error instanceof NotFoundError) return {}; | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| export async function generateStaticParams() { | ||
| const sitePosts = await client.queries.pagesConnection({}); | ||
| return ( | ||
| sitePosts.data.pagesConnection?.edges?.map((post) => { | ||
| const breadcrumbs = post?.node?._sys.breadcrumbs ?? []; | ||
| return { | ||
| locale: localeFromBreadcrumbs(breadcrumbs), | ||
| product: breadcrumbs[0], | ||
| filename: post?.node?._sys.filename, | ||
| }; | ||
| }) || [] | ||
| ); | ||
| } | ||
|
|
||
| export default async function FilePage({ params }: FilePageProps) { | ||
| const { locale, product, filename } = await params; | ||
| try { | ||
| const { data, query, relativePath } = await getPageData(product, filename, locale); | ||
| return <HomePageClient query={query} data={data} variables={{ relativePath }} />; | ||
| } catch (error) { | ||
| if (error instanceof NotFoundError) { | ||
| return ( | ||
| <ClientFallbackPage | ||
| product={product} | ||
| relativePath={filename} | ||
| query="getPageData" | ||
| Component={HomePageClient} | ||
| /> | ||
| ); | ||
| } | ||
| notFound(); | ||
| } | ||
| } | ||
|
|
||
| export const revalidate = 3600; |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import client from "@tina/__generated__/client"; | ||
| import { setPageMetadata } from "@utils/setPageMetaData"; | ||
| import { getBlogWithFallback } from "@utils/i18n"; | ||
| import getBlogPageData from "@utils/pages/getBlogPageData"; | ||
| import ClientFallbackPage from "@app/client-fallback-page"; | ||
| import NotFoundError from "@/errors/not-found"; | ||
| import { BlogPageShared, BlogPageSharedProps } from "./blog-shared"; | ||
| import { localeFromBreadcrumbs } from "@utils/localeFromBreadcrumbs"; | ||
|
|
||
| interface BlogPostProps { | ||
| params: Promise<{ locale: string; slug: string; product: string }>; | ||
| } | ||
|
|
||
| export async function generateMetadata({ params }: BlogPostProps) { | ||
| const { locale, slug, product } = await params; | ||
| try { | ||
| const res = await getBlogWithFallback({ product, slug, locale }); | ||
| if (!res?.data?.blogs) return null; | ||
| return setPageMetadata(res?.data?.blogs?.seo, product, "Blog"); | ||
| } catch (error) { | ||
| if (error instanceof NotFoundError) return {}; | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| export async function generateStaticParams() { | ||
| const sitePosts = await client.queries.blogsConnection({}); | ||
| return ( | ||
| sitePosts.data.blogsConnection?.edges?.map((post) => { | ||
| const breadcrumbs = post?.node?._sys.breadcrumbs ?? []; | ||
| return { | ||
| locale: localeFromBreadcrumbs(breadcrumbs), | ||
| product: breadcrumbs[0], | ||
| slug: post?.node?._sys.filename, | ||
| }; | ||
| }) || [] | ||
| ); | ||
| } | ||
|
|
||
| export default async function BlogPost({ params }: BlogPostProps) { | ||
| const { locale, slug, product } = await params; | ||
| try { | ||
| const data = await getBlogPageData(product, slug, locale); | ||
| return <BlogPageShared {...data} />; | ||
| } catch (error) { | ||
| if (error instanceof NotFoundError) { | ||
| return ( | ||
| <ClientFallbackPage<BlogPageSharedProps> | ||
| product={product} | ||
| relativePath={slug} | ||
| query={"getBlogPageData"} | ||
| Component={BlogPageShared} | ||
| /> | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { getDocPost } from "@utils/fetchDocs"; | ||
| import client from "@tina/__generated__/client"; | ||
| import { setPageMetadata } from "@utils/setPageMetaData"; | ||
| import DocPostClient from "./DocPostClient"; | ||
| import getDocPageData from "@utils/pages/getDocPageData"; | ||
| import ClientFallbackPage from "@app/client-fallback-page"; | ||
| import NotFoundError from "@/errors/not-found"; | ||
| import { localeFromBreadcrumbs } from "@utils/localeFromBreadcrumbs"; | ||
|
|
||
| interface DocPostProps { | ||
| params: Promise<{ locale: string; slug: string; product: string }>; | ||
| } | ||
|
|
||
| export async function generateMetadata({ params }: DocPostProps) { | ||
| const { locale, product, slug } = await params; | ||
| try { | ||
| const docs = await getDocPost({ product, slug, locale }); | ||
| return setPageMetadata(docs?.docs?.seo, product, "Docs"); | ||
| } catch (error) { | ||
| if (error instanceof NotFoundError) return {}; | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| export async function generateStaticParams() { | ||
| const sitePosts = await client.queries.docsConnection({}); | ||
| return ( | ||
| sitePosts.data.docsConnection?.edges?.map((post) => { | ||
| const breadcrumbs = post?.node?._sys.breadcrumbs ?? []; | ||
| return { | ||
| locale: localeFromBreadcrumbs(breadcrumbs), | ||
| product: breadcrumbs[0], | ||
| slug: post?.node?._sys.filename, | ||
| }; | ||
| }) || [] | ||
| ); | ||
| } | ||
|
|
||
| export default async function DocPost({ params }: DocPostProps) { | ||
| const { locale, slug, product } = await params; | ||
| try { | ||
| const documentData = await getDocPageData({ product, slug, locale }); | ||
| return <DocPostClient {...documentData} />; | ||
| } catch (error) { | ||
| if (error instanceof NotFoundError) { | ||
| return ( | ||
| <ClientFallbackPage | ||
| product={product} | ||
| relativePath={slug} | ||
| query="getDocPageData" | ||
| Component={DocPostClient} | ||
| /> | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| export const revalidate = 3600; |
9 changes: 2 additions & 7 deletions
9
app/[product]/docs/layout.tsx → app/[locale]/[product]/docs/layout.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generateStaticParams is copy-pasted across three siblings. This block, plus the equivalents in
docs/[slug]/page.tsxand[filename]/page.tsx, are structurally identical: query a*Connection, then map edges to{ locale: localeFromBreadcrumbs(bc), product: bc[0], <leaf>: filename }. Only the client query and the leaf key name differ. Thepage.tsx/blog/page.tsx/docs/page.tsxindex pages also repeat the sameseen/paramsdedup loop verbatim.Worth pulling into one helper in
utils/, e.g.staticParamsFromConnection(edges, leafKey), so each page becomes a one-liner and there is a single place to fix a future copy-paste bug. Non-blocking.