-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
App Router: Support user-defined paths for predefined pages (#2293)
This is achieved by using a combination of rewrites and redirects in the middleware: - Rewrite from the page tree node path (e.g., `/aktuelles`) to the code path (e.g., `/news`) - Redirect from the code path (e.g., `/news`) to the page tree node path (e.g., `/aktuelles`)
- Loading branch information
1 parent
bc1ed88
commit f1d9e44
Showing
10 changed files
with
120 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
--- | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
Support filtering for document types in the `paginatedPageTreeNodes` query | ||
|
||
**Example** | ||
|
||
```graphql | ||
query PredefinedPages($scope: PageTreeNodeScopeInput!) { | ||
paginatedPageTreeNodes(scope: $scope, documentType: "PredefinedPage") { | ||
nodes { | ||
id | ||
} | ||
} | ||
} | ||
``` |
This file contains 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
This file contains 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
This file contains 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.
This file contains 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,77 @@ | ||
import { gql } from "@comet/cms-site"; | ||
import { languages } from "@src/config"; | ||
import { predefinedPagePaths } from "@src/predefinedPages/predefinedPagePaths"; | ||
import { createGraphQLFetch } from "@src/util/graphQLClient"; | ||
|
||
import { memoryCache } from "./cache"; | ||
import { GQLPredefinedPagesQuery, GQLPredefinedPagesQueryVariables } from "./predefinedPages.generated"; | ||
|
||
async function getPredefinedPageRedirect(scope: { domain: string }, pathname: string): Promise<string | undefined> { | ||
const pages = await fetchPredefinedPages(scope); | ||
|
||
const matchingPredefinedPage = pages.find((page) => pathname.startsWith(page.codePath)); | ||
|
||
if (matchingPredefinedPage) { | ||
return pathname.replace(matchingPredefinedPage.codePath, matchingPredefinedPage.pageTreeNodePath); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
async function getPredefinedPageRewrite(scope: { domain: string }, pathname: string): Promise<string | undefined> { | ||
const pages = await fetchPredefinedPages(scope); | ||
|
||
const matchingPredefinedPage = pages.find((page) => pathname.startsWith(page.pageTreeNodePath)); | ||
|
||
if (matchingPredefinedPage) { | ||
return pathname.replace(matchingPredefinedPage.pageTreeNodePath, matchingPredefinedPage.codePath); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
const predefinedPagesQuery = gql` | ||
query PredefinedPages($scope: PageTreeNodeScopeInput!) { | ||
paginatedPageTreeNodes(scope: $scope, documentType: "PredefinedPage") { | ||
nodes { | ||
id | ||
path | ||
document { | ||
__typename | ||
... on PredefinedPage { | ||
type | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const graphQLFetch = createGraphQLFetch(); | ||
|
||
async function fetchPredefinedPages(scope: { domain: string }) { | ||
const key = `predefinedPages-${JSON.stringify(scope)}`; | ||
|
||
return memoryCache.wrap(key, async () => { | ||
const pages: Array<{ codePath: string; pageTreeNodePath: string }> = []; | ||
|
||
for (const language of languages) { | ||
const { paginatedPageTreeNodes } = await graphQLFetch<GQLPredefinedPagesQuery, GQLPredefinedPagesQueryVariables>(predefinedPagesQuery, { | ||
scope: { domain: scope.domain, language }, | ||
}); | ||
|
||
for (const node of paginatedPageTreeNodes.nodes) { | ||
if (node.document?.__typename === "PredefinedPage" && node.document.type) { | ||
pages.push({ | ||
codePath: `/${language}${predefinedPagePaths[node.document.type]}`, | ||
pageTreeNodePath: `/${language}${node.path}`, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return pages; | ||
}); | ||
} | ||
|
||
export { getPredefinedPageRedirect, getPredefinedPageRewrite }; |
File renamed without changes.
This file contains 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
This file contains 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
This file contains 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