Skip to content
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

feat: static rendering #825

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading