Skip to content

Commit

Permalink
Add resolvedUrl filter for modifying the resolved URL in the Faust …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
blakewilson authored Apr 19, 2023
1 parent c6e0ac4 commit 4a4ef79
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-impalas-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/core': patch
---

Add `resolvedUrl` filter for modifying the resolved URL in the Faust template system
24 changes: 24 additions & 0 deletions internal/faustjs.org/docs/plugin-system/filters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
4 changes: 4 additions & 0 deletions packages/faustwp-core/src/getWordPressProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions packages/faustwp-core/src/wpHooks/overloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;

1 comment on commit 4a4ef79

@headless-platform-by-wp-engine

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out the recent updates to your Atlas environment:

App Environment URL Build
faustjs canary https://hg…wered.com ✅ (logs)

Learn more about building on Atlas in our documentation.

Please sign in to comment.