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

Flagged SSR support #2548

Merged
merged 14 commits into from
Feb 14, 2022
Prev Previous commit
Next Next commit
Fixes population of getStaticPaths results
  • Loading branch information
matthewp committed Feb 10, 2022
commit b8b384f14e0014a1fbd8f48457367c2d2c96ce70
24 changes: 19 additions & 5 deletions packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import type { ComponentInstance, MarkdownRenderOptions, Params, Props, Renderer, RouteData, SSRElement } from '../../@types/astro';
import { LogOptions } from '../logger.js';
import type { LogOptions } from '../logger.js';

import { renderPage } from '../../runtime/server/index.js';
import { getParams } from '../routing/index.js';
import { createResult } from './result.js';
import { findPathItemByKey, RouteCache } from './route-cache.js';
import { findPathItemByKey, RouteCache, callGetStaticPaths } from './route-cache.js';
import { warn } from '../logger.js';

export async function getParamsAndProps({ route, routeCache, pathname }: { route: RouteData | undefined; routeCache: RouteCache; pathname: string }): Promise<[Params, Props]> {
interface GetParamsAndPropsOptions {
mod: ComponentInstance;
route: RouteData | undefined;
routeCache: RouteCache;
pathname: string;
logging: LogOptions;
}

async function getParamsAndProps(opts: GetParamsAndPropsOptions): Promise<[Params, Props]> {
const { logging, mod, route, routeCache, pathname } = opts;
// Handle dynamic routes
let params: Params = {};
let pageProps: Props;
Expand All @@ -17,9 +27,11 @@ export async function getParamsAndProps({ route, routeCache, pathname }: { route
params = getParams(route.params)(paramsMatch);
}
}
const routeCacheEntry = routeCache.get(route);
let routeCacheEntry = routeCache.get(route);
if (!routeCacheEntry) {
throw new Error(`[${route.component}] Internal error: route cache was empty, but expected to be full.`);
warn(logging, 'routeCache', `Internal Warning: getStaticPaths() called twice during the build. (${route.component})`);
routeCacheEntry = await callGetStaticPaths(mod, route, true, logging);
routeCache.set(route, routeCacheEntry);
}
const paramsKey = JSON.stringify(params);
const matchedStaticPath = findPathItemByKey(routeCacheEntry.staticPaths, paramsKey);
Expand Down Expand Up @@ -69,6 +81,8 @@ export async function render(opts: RenderOptions): Promise<string> {
} = opts;

const [params, pageProps] = await getParamsAndProps({
logging,
mod,
route,
routeCache,
pathname,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/dev/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fileURLToPath } from 'url';
let hmrScript: string;
export async function getHmrScript() {
if (hmrScript) return hmrScript;
const filePath = fileURLToPath(new URL('../../runtime/client/hmr.js', import.meta.url));
const filePath = fileURLToPath(new URL('../../../runtime/client/hmr.js', import.meta.url));
const content = await fs.promises.readFile(filePath);
hmrScript = content.toString();
return hmrScript;
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/core/render/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export async function preload({ astroConfig, filePath, viteServer }: SSROptions)
export async function render(renderers: Renderer[], mod: ComponentInstance, ssrOpts: SSROptions): Promise<string> {
const { astroConfig, filePath, logging, mode, origin, pathname, route, routeCache, viteServer } = ssrOpts;


// Add hoisted script tags
const scripts = createModuleScriptElementWithSrcSet(astroConfig.buildOptions.experimentalStaticBuild ?
Array.from(mod.$$metadata.hoistedScriptPaths()) :
Expand Down