From 4a4ef79892ca959bc12e46f192a16d052929a153 Mon Sep 17 00:00:00 2001 From: Blake Wilson Date: Wed, 19 Apr 2023 15:27:19 -0500 Subject: [PATCH] Add `resolvedUrl` filter for modifying the resolved URL in the Faust template system (#1391) * Add filter for resolved URL in template system * Add overload signature for `resolvedUrl` filter * Add changeset * Fix imports order * Add `resolvedUrl` filter to docs * Fix context object type in docs --- .changeset/swift-impalas-kneel.md | 5 ++++ .../docs/plugin-system/filters.mdx | 24 +++++++++++++++++++ .../faustwp-core/src/getWordPressProps.tsx | 4 ++++ .../faustwp-core/src/wpHooks/overloads.ts | 13 ++++++++++ 4 files changed, 46 insertions(+) create mode 100644 .changeset/swift-impalas-kneel.md diff --git a/.changeset/swift-impalas-kneel.md b/.changeset/swift-impalas-kneel.md new file mode 100644 index 000000000..a1555babf --- /dev/null +++ b/.changeset/swift-impalas-kneel.md @@ -0,0 +1,5 @@ +--- +'@faustwp/core': patch +--- + +Add `resolvedUrl` filter for modifying the resolved URL in the Faust template system diff --git a/internal/faustjs.org/docs/plugin-system/filters.mdx b/internal/faustjs.org/docs/plugin-system/filters.mdx index d28023664..d97bac8df 100644 --- a/internal/faustjs.org/docs/plugin-system/filters.mdx +++ b/internal/faustjs.org/docs/plugin-system/filters.mdx @@ -180,3 +180,27 @@ Allows you to modify Faust's Toolbar nodes. priority?: number | undefined, ): void; ``` + +## resolvedUrl + +Allows you to override the resolved URL in the Faust template system. + +### Context Object + +- `nextContext: GetServerSidePropsContext | GetStaticPropsContext`: The Next.js context object from either `getServerSideProps`or`getStaticProps` + +### Signature + +```tsx +addFilter( + hookName: 'resolvedUrl', + namespace: string, + callback: ( + resolvedUrl: string | null, + context: { + nextContext: GetServerSidePropsContext | GetStaticPropsContext; + }, + ) => string | null, + priority?: number | undefined, +): void; +``` diff --git a/packages/faustwp-core/src/getWordPressProps.tsx b/packages/faustwp-core/src/getWordPressProps.tsx index bd04781dd..9845d2009 100644 --- a/packages/faustwp-core/src/getWordPressProps.tsx +++ b/packages/faustwp-core/src/getWordPressProps.tsx @@ -71,6 +71,10 @@ export async function getWordPressProps( ctx.res.setHeader('x-using', 'faust'); } + resolvedUrl = hooks.applyFilters('resolvedUrl', resolvedUrl, { + nextContext: ctx, + }) as string | null; + if (!resolvedUrl) { return { notFound: true, diff --git a/packages/faustwp-core/src/wpHooks/overloads.ts b/packages/faustwp-core/src/wpHooks/overloads.ts index 57c30830c..74bddbeda 100644 --- a/packages/faustwp-core/src/wpHooks/overloads.ts +++ b/packages/faustwp-core/src/wpHooks/overloads.ts @@ -5,6 +5,7 @@ import { NormalizedCacheObject, } from '@apollo/client'; import { _Hooks } from '@wordpress/hooks/build-types/createHooks.js'; +import { GetServerSidePropsContext, GetStaticPropsContext } from 'next'; import { FaustToolbarNodes } from '../components/Toolbar/index.js'; import { SeedNode } from '../queries/seedQuery.js'; @@ -79,6 +80,18 @@ type FaustCoreFilters = { ) => string, priority?: number | undefined, ): void; + + addFilter( + hookName: 'resolvedUrl', + namespace: string, + callback: ( + resolvedUrl: string | null, + context: { + nextContext: GetServerSidePropsContext | GetStaticPropsContext; + }, + ) => string | null, + priority?: number | undefined, + ): void; }; export type FaustHooks = _Hooks & FaustCoreFilters;