Skip to content

Commit

Permalink
feat: static rendering (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio authored Jul 26, 2024
1 parent 9f05f3f commit 4044248
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 122 deletions.
6 changes: 6 additions & 0 deletions .changeset/tall-peas-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@headstartwp/core": patch
"@headstartwp/next": patch
---

Introducing `loadHeadstartWpConfig()`
211 changes: 106 additions & 105 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 9 additions & 12 deletions packages/next/src/rsc/config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { getHeadstartWPConfig, getSiteByHost } from '@headstartwp/core';
import { headers } from 'next/headers';
import { HeadstartWPRoute } from './types';
import { prepareQuery } from './data/queries/prepareQuery';

/**
* How to make this work in edge runtimes?
* Loads the right config based on route params
*
* @param routeParams The next.js route params
*
* @returns
*/
export async function loadHeadstartWPConfig() {
const config = getHeadstartWPConfig();
const headersList = headers();

const site = headersList.get('x-headstartwp-site');

if (site) {
return getSiteByHost(site);
}
export function loadHeadstartWPConfig(routeParams: HeadstartWPRoute['params']) {
const { config } = prepareQuery({ routeParams });

return config;
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('prepareQuery with lang and polylang', () => {
getHeadstartWPConfig(),
),
).toThrow(
'Unsuported lang, make sure you add all desired locales to `config.i18n.locales`',
'Unsuported lang (br), make sure you add all desired locales to "config.i18n.locales"',
);
});
});
2 changes: 1 addition & 1 deletion packages/next/src/rsc/data/queries/prepareQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function prepareQuery<P extends EndpointParams>(
const supportedLocales = rootConfig.i18n?.locales ?? [];
if (!supportedLocales.includes(routeParams.lang)) {
throw new FrameworkError(
'Unsuported lang, make sure you add all desired locales to `config.i18n.locales`',
`Unsuported lang (${routeParams.lang}), make sure you add all desired locales to "config.i18n.locales"`,
);
}
params.lang = routeParams.lang;
Expand Down
2 changes: 2 additions & 0 deletions projects/wp-multisite-nextjs-app/src/app/api/preview/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { previewRouteHandler } from '@headstartwp/next/app';
import type { NextRequest } from 'next/server';

export const dynamic = 'force-dynamic';

export async function GET(request: NextRequest) {
// @ts-expect-error
return previewRouteHandler(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { revalidateRouteHandler } from '@headstartwp/next/app';
import type { NextRequest } from 'next/server';

export const dynamic = 'force-dynamic';

export async function GET(request: NextRequest) {
// @ts-expect-error
return revalidateRouteHandler(request);
Expand Down
29 changes: 28 additions & 1 deletion projects/wp-nextjs-app/src/app/(single)/[...path]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import { HtmlDecoder } from '@headstartwp/core/react';
import { HeadstartWPRoute, JSONLD, queryPost } from '@headstartwp/next/app';
import {
HeadstartWPRoute,
JSONLD,
loadHeadstartWPConfig,
queryPost,
queryPosts,
} from '@headstartwp/next/app';
import { Metadata } from 'next';
import { removeSourceUrl } from '@headstartwp/core';
import Blocks from '../../../components/Blocks';

export async function generateStaticParams({ params }: HeadstartWPRoute) {
// loads the right config based on route params (this is needed over getHeadstartWP for sites using polylang integration or multisite)
const { sourceUrl = '', hostUrl = '/' } = loadHeadstartWPConfig(params);

const { data } = await queryPosts({
routeParams: params,
params: { postType: 'post' },
});

return data.posts.map((post) => ({
path: removeSourceUrl({
backendUrl: sourceUrl,
link: post.link,
publicUrl: hostUrl,
})
.substring(1)
.split('/'),
}));
}

async function query({ params }: HeadstartWPRoute) {
return queryPost({
routeParams: params,
Expand Down
2 changes: 2 additions & 0 deletions projects/wp-nextjs-app/src/app/api/preview/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { previewRouteHandler } from '@headstartwp/next/app';
import type { NextRequest } from 'next/server';

export const dynamic = 'force-dynamic';

export async function GET(request: NextRequest) {
// @ts-expect-error
return previewRouteHandler(request);
Expand Down
2 changes: 2 additions & 0 deletions projects/wp-nextjs-app/src/app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { revalidateRouteHandler } from '@headstartwp/next/app';
import type { NextRequest } from 'next/server';

export const dynamic = 'force-dynamic';

export async function GET(request: NextRequest) {
// @ts-expect-error
return revalidateRouteHandler(request);
Expand Down
4 changes: 3 additions & 1 deletion projects/wp-nextjs-app/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ async function query({ params }: HeadstartWPRoute) {
params: {
slug: 'sample-page',
postType: 'page',
matchCurrentPath: false,
},
options: {
cache: 'force-cache',
},
});
}
Expand Down
Loading

0 comments on commit 4044248

Please sign in to comment.