Skip to content

Commit

Permalink
feat: withSiteContext
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Oct 14, 2024
1 parent 7446f55 commit 52a5cb7
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
5 changes: 5 additions & 0 deletions packages/next/src/data/server/__tests__/addHookData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('addHookData', () => {
pageInfo: { ...samplePageInfo },
},
isMainQuery: true,
hostOrSlug: 'mainsite',
},
{
key: 'second-key',
Expand All @@ -74,10 +75,12 @@ describe('addHookData', () => {
pageInfo: { ...samplePageInfo },
},
isMainQuery: false,
hostOrSlug: 'site2',
},
];
expect(addHookData(hookStates, {})).toStrictEqual({
props: {
__headstartwp_site: 'mainsite',
fallback: {
'first-key': {
result: {
Expand Down Expand Up @@ -120,6 +123,7 @@ describe('addHookData', () => {
pageInfo: samplePageInfo,
},
isMainQuery: false,
hostOrSlug: 'site2',
},
];

Expand All @@ -135,6 +139,7 @@ describe('addHookData', () => {

expect(addHookData(hookStates, {})).toStrictEqual({
props: {
__headstartwp_site: 'site2',
fallback: {
'first-key': {
queriedObject: {},
Expand Down
26 changes: 26 additions & 0 deletions packages/next/src/data/server/__tests__/getSiteFromContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { setHeadstartWPConfig } from '@headstartwp/core';
import { getSiteFromContext } from '../getSiteFromContext';

describe('getSiteFromContext', () => {
it('returns settings if no site is provided', () => {
const ctx = { params: { site: 'site2' } };
setHeadstartWPConfig({
useWordPressPlugin: true,
sites: [
{
slug: 'site1',
hostUrl: 'http://site1.localhost:3001',
sourceUrl: 'http://sourceUrl1.com',
redirectStrategy: '404',
},
{
slug: 'site2',
hostUrl: 'http://site2.localhost:3001',
sourceUrl: 'http://sourceUrl2.com',
redirectStrategy: '404',
},
],
});
expect(getSiteFromContext(ctx).slug).toBe('site2');
});
});
12 changes: 12 additions & 0 deletions packages/next/src/data/server/__tests__/withSiteContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { withSiteContext } from '../withSiteContext';

describe('withSiteContext', () => {
it('adds site to params', () => {
expect(withSiteContext({ params: { id: 1 } }, 'site1')).toEqual({
params: { id: 1, site: 'site1' },
});
expect(withSiteContext({ params: { id: 1, site: 'site2' } }, 'site1')).toEqual({
params: { id: 1, site: 'site1' },
});
});
});
4 changes: 2 additions & 2 deletions packages/next/src/data/server/getSiteFromContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getHeadlessConfig, getSite, getSiteByHost } from '@headstartwp/core';
import { getHeadstartWPConfig, getSite, getSiteByHost } from '@headstartwp/core';
import { GetServerSidePropsContext, GetStaticPropsContext } from 'next';

/**
Expand All @@ -9,7 +9,7 @@ import { GetServerSidePropsContext, GetStaticPropsContext } from 'next';
*/
export function getSiteFromContext(ctx: GetServerSidePropsContext | GetStaticPropsContext) {
const currentSite = ctx?.params?.site;
const settings = getHeadlessConfig();
const settings = getHeadstartWPConfig();

if (currentSite && typeof currentSite === 'string') {
const site = getSiteByHost(currentSite);
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from '../convertToPath';
export * from './fetchHookData';
export * from './getSiteFromContext';
export * from './handleError';
export * from './withSiteContext';
15 changes: 15 additions & 0 deletions packages/next/src/data/server/withSiteContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { GetServerSidePropsContext, GetStaticPropsContext } from 'next';
import { PreviewData } from '../../handlers/types';

export function withSiteContext(
ctx: GetServerSidePropsContext<any, PreviewData> | GetStaticPropsContext<any, PreviewData>,
site: string,
) {
return {
...ctx,
params: {
...(ctx.params ?? {}),
site,
},
};
}
10 changes: 6 additions & 4 deletions projects/wp-multisite-nextjs/src/pages/_sites/site1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import {
handleError,
useAppSettings,
usePost,
withSiteContext,
} from '@headstartwp/next';
import { useSettings } from '@headstartwp/core/react';
import { indexParams } from '../../../params';

const Site1Homepage = () => {
return <div>Site 1 Homepage</div>;
const settings = useSettings();
return <div>{settings.slug} Homepage</div>;
};

export default Site1Homepage;

export async function getStaticProps(context) {
// TODO: create a utlity function to decorate context for a specific site
context.params = { site: 'site1' };
export async function getStaticProps(_context) {
const context = withSiteContext(_context, 'site1');

let appSettings;
let slug;
Expand Down

0 comments on commit 52a5cb7

Please sign in to comment.