Skip to content

Commit

Permalink
feat: pass rootConfig as context to getStaticPaths (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenle authored Dec 12, 2023
1 parent 2455c96 commit 1f3ab3c
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 134 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-kiwis-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root': patch
---

feat: pass rootConfig as context to getStaticPaths
2 changes: 1 addition & 1 deletion packages/root-cms/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class RootCMSClient {

constructor(rootConfig: RootConfig) {
this.rootConfig = rootConfig;
this.cmsPlugin = getCmsPlugin(rootConfig);
this.cmsPlugin = getCmsPlugin(this.rootConfig);

const cmsPluginOptions = this.cmsPlugin.getConfig();
this.projectId = cmsPluginOptions.id || 'default';
Expand Down
12 changes: 7 additions & 5 deletions packages/root/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export type GetStaticProps<T = unknown> = (ctx: {
* paths that exist for a given route. This should be used alongside a
* parameterized route, e.g. `/routes/blog/[slug].tsx`.
*/
export type GetStaticPaths<T = RouteParams> = () => Promise<{
export type GetStaticPaths<T = RouteParams> = (ctx: {
rootConfig: RootConfig;
}) => Promise<{
paths: Array<{params: T}>;
}>;

Expand Down Expand Up @@ -117,7 +119,7 @@ export type NextFunction = ExpressNextFunction;
* A context variable passed to a route's `handle()` method within the req
* object.
*/
export interface HandlerContext<T = any> {
export interface HandlerContext<Props = any> {
/**
* The resolved route.
*/
Expand All @@ -139,7 +141,7 @@ export interface HandlerContext<T = any> {
*/
getPreferredLocale: (availableLocales: string[]) => string;
/** Renders the default exported component from the route. */
render: HandlerRenderFn;
render: HandlerRenderFn<Props>;
/** Renders a 404 page. */
render404: () => Promise<void>;
}
Expand Down Expand Up @@ -210,7 +212,7 @@ export interface HandlerRenderOptions {
translations?: Record<string, string>;
}

export type HandlerRenderFn = (
props: any,
export type HandlerRenderFn<Props = any> = (
props: Props,
options?: HandlerRenderOptions
) => Promise<void>;
21 changes: 11 additions & 10 deletions packages/root/src/render/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import {AssetMap} from './asset-map/asset-map';
import {htmlMinify} from './html-minify';
import {htmlPretty} from './html-pretty';
import {getFallbackLocales} from './i18n-fallbacks';
import {RouteTrie} from './route-trie';
import {getRoutes, getAllPathsForRoute, replaceParams} from './router';
import {replaceParams, Router} from './router';

interface RenderHtmlOptions {
/** Attrs passed to the <html> tag, e.g. `{lang: 'en'}`. */
Expand All @@ -41,23 +40,25 @@ interface RenderHtmlOptions {

export class Renderer {
private rootConfig: RootConfig;
private routes: RouteTrie<Route>;
// private routes: RouteTrie<Route>;
private assetMap: AssetMap;
private elementGraph: ElementGraph;
private router: Router;

constructor(
rootConfig: RootConfig,
options: {assetMap: AssetMap; elementGraph: ElementGraph}
) {
this.rootConfig = rootConfig;
this.routes = getRoutes(this.rootConfig);
// this.routes = getRoutes(this.rootConfig);
this.assetMap = options.assetMap;
this.elementGraph = options.elementGraph;
this.router = new Router(rootConfig);
}

async handle(req: Request, res: Response, next: NextFunction) {
const url = req.path;
const [route, routeParams] = this.routes.get(url);
const [route, routeParams] = this.router.get(url);
if (!route) {
next();
return;
Expand Down Expand Up @@ -313,8 +314,8 @@ export class Renderer {
string,
{route: Route; params: Record<string, string>}
> = {};
await this.routes.walk(async (urlPath: string, route: Route) => {
const routePaths = await getAllPathsForRoute(urlPath, route);
await this.router.walk(async (urlPath: string, route: Route) => {
const routePaths = await this.router.getAllPathsForRoute(urlPath, route);
routePaths.forEach((routePath) => {
sitemap[routePath.urlPath] = {
route,
Expand Down Expand Up @@ -343,7 +344,7 @@ export class Renderer {

async render404(options?: {currentPath?: string}) {
const currentPath = options?.currentPath || '/404';
const [route, routeParams] = this.routes.get('/404');
const [route, routeParams] = this.router.get('/404');
if (route && route.src === 'routes/404.tsx' && route.module.default) {
const Component = route.module.default;
return this.renderComponent(
Expand Down Expand Up @@ -375,7 +376,7 @@ export class Renderer {

async renderError(err: any, options?: {currentPath?: string}) {
const currentPath = options?.currentPath || '/500';
const [route, routeParams] = this.routes.get('/500');
const [route, routeParams] = this.router.get('/500');
if (route && route.src === 'routes/500.tsx' && route.module.default) {
const Component = route.module.default;
return this.renderComponent(
Expand Down Expand Up @@ -417,7 +418,7 @@ export class Renderer {
}

async renderDevServer500(req: Request, error: unknown) {
const [route, routeParams] = this.routes.get(req.path);
const [route, routeParams] = this.router.get(req.path);
const mainHtml = renderToString(
<DevErrorPage
req={req}
Expand Down
Loading

0 comments on commit 1f3ab3c

Please sign in to comment.