Skip to content

Commit

Permalink
feat: get assets
Browse files Browse the repository at this point in the history
  • Loading branch information
answershuto committed Nov 22, 2023
1 parent 105d5d2 commit d25d94b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
30 changes: 6 additions & 24 deletions packages/runtime/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ const Context = React.createContext<DocumentContext | undefined>(undefined);

Context.displayName = 'DocumentContext';

// Collect render assets for SSR preRender.
function pushRenderAssets(asset: Array<string> | string) {
if (typeof window === 'undefined') return;
if (!window.renderAssets) {
window.renderAssets = [];
}

if (Array.isArray(asset)) {
window.renderAssets.push(...asset);
} else {
window.renderAssets.push(asset);
}
}

function useDocumentContext() {
const value = React.useContext(Context);
return value;
Expand Down Expand Up @@ -97,10 +83,6 @@ export const Links: LinksType = (props: LinksProps) => {
const entryAssets = getEntryAssets(assetsManifest);
const styles = entryAssets.concat(pageAssets).filter(path => path.indexOf('.css') > -1);

// Collect styles.
pushRenderAssets(styles);
pushRenderAssets(routeLinks.map(routeLink => routeLink.href));

return (
<>
{
Expand Down Expand Up @@ -144,10 +126,6 @@ export const Scripts: ScriptsType = (props: ScriptsProps) => {
return true;
});

// Collect scripts.
pushRenderAssets(routeScripts.map(routeScriptProps => routeScriptProps.src));
pushRenderAssets(scripts);

return (
<>
<Data ScriptElement={ScriptElement} />
Expand All @@ -165,8 +143,7 @@ export const Scripts: ScriptsType = (props: ScriptsProps) => {
);
};

export function usePageAssets() {
const { loaderData, matches, assetsManifest } = useAppContext();
export function getAllAssets(loaderData, matches, assetsManifest): Array<string> {
const routeLinks = getLinks(matches, loaderData);
const routeScripts = getScripts(matches, loaderData);
const pageAssets = getPageAssets(matches, assetsManifest);
Expand All @@ -181,6 +158,11 @@ export function usePageAssets() {
return assets;
}

export function usePageAssets() {
const { loaderData, matches, assetsManifest } = useAppContext();
return getAllAssets(loaderData, matches, assetsManifest);
}

interface DataProps {
ScriptElement?: React.ComponentType<React.ScriptHTMLAttributes<HTMLScriptElement>> | string;
}
Expand Down
12 changes: 8 additions & 4 deletions packages/runtime/src/runServerApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { isFunction } from '@ice/shared';
import type { RenderToPipeableStreamOptions, OnAllReadyParams } from './server/streamRender.js';
import type {
AppContext, RouteItem, ServerContext,
AppExport, AssetsManifest,
AppExport,
AssetsManifest,
RouteMatch,
PageConfig,
RenderMode,
Expand All @@ -21,7 +22,7 @@ import Runtime from './runtime.js';
import { AppContextProvider } from './AppContext.js';
import { getAppData } from './appData.js';
import getAppConfig from './appConfig.js';
import { DocumentContextProvider } from './Document.js';
import { DocumentContextProvider, usePageAssets } from './Document.js';

Check warning on line 25 in packages/runtime/src/runServerApp.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

'usePageAssets' is defined but never used. Allowed unused vars must match /[iI]gnored|createElement/u

Check warning on line 25 in packages/runtime/src/runServerApp.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, windows-latest)

'usePageAssets' is defined but never used. Allowed unused vars must match /[iI]gnored|createElement/u

Check warning on line 25 in packages/runtime/src/runServerApp.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

'usePageAssets' is defined but never used. Allowed unused vars must match /[iI]gnored|createElement/u

Check warning on line 25 in packages/runtime/src/runServerApp.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

'usePageAssets' is defined but never used. Allowed unused vars must match /[iI]gnored|createElement/u
import { loadRouteModules } from './routes.js';
import type { RouteLoaderOptions } from './routes.js';
import { pipeToString, renderToNodeStream } from './server/streamRender.js';
Expand All @@ -34,7 +35,7 @@ import { renderHTMLToJS } from './renderHTMLToJS.js';
import addLeadingSlash from './utils/addLeadingSlash.js';


interface RenderOptions {
export interface RenderOptions {
app: AppExport;
assetsManifest: AssetsManifest;
createRoutes: (options: Pick<RouteLoaderOptions, 'requestContext' | 'renderMode'>) => RouteItem[];
Expand Down Expand Up @@ -416,7 +417,10 @@ async function renderServerEntry(
</AppContextProvider>
);

const pipe = renderToNodeStream(element);
const pipe = renderToNodeStream(element, {
renderOptions,
routerContext,
});

const fallback = () => {
return renderDocument({
Expand Down
29 changes: 28 additions & 1 deletion packages/runtime/src/server/streamRender.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as Stream from 'stream';
import type * as StreamType from 'stream';
import * as ReactDOMServer from 'react-dom/server';
import { getAllAssets } from '../Document.js';
import type { RenderOptions } from '../runServerApp.js';
import type { ServerAppRouterProps } from '../types.js';

const { Writable } = Stream;

Expand All @@ -20,8 +23,13 @@ export type NodeWritablePiper = (
options?: RenderToPipeableStreamOptions,
) => void;

export type RenderToNodeStreamOptions = {
renderOptions: RenderOptions;
routerContext: ServerAppRouterProps['routerContext'];
};
export function renderToNodeStream(
element: React.ReactElement,
renderToNodeStreamOptions: RenderToNodeStreamOptions,
): NodeWritablePiper {
return (res, options) => {
const { pipe } = ReactDOMServer.renderToPipeableStream(element, {
Expand All @@ -36,8 +44,27 @@ export function renderToNodeStream(
options?.onError && options?.onError(error);
},
onAllReady() {
const {
renderOptions,
routerContext,
} = renderToNodeStreamOptions;

const {
assetsManifest,
} = renderOptions;

const {
matches,
loaderData,
} = routerContext;

const renderAssets = getAllAssets(loaderData, matches, assetsManifest);
if (typeof window !== 'undefined' && window.renderAssets) {
renderAssets.concat(window.renderAssets);
}

options?.onAllReady && options?.onAllReady({
renderAssets: (typeof window === 'undefined') ? [] : (window.renderAssets || []),
renderAssets,
});
},
});
Expand Down

0 comments on commit d25d94b

Please sign in to comment.