Skip to content

Commit

Permalink
Add preloads to default HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade committed Oct 19, 2024
1 parent eb2b1f8 commit 4df5ab6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/preact-browser/source/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export {
HTMLPlaceholderContent,
HTMLPlaceholderAsyncAssets,
HTMLPlaceholderEntryAssets,
HTMLPlaceholderPreloadAssets,
HTMLPlaceholderSerializations,
} from './server/components/HTMLPlaceholder.tsx';
export {ResponseStreamBoundary} from './server/components/ResponseStreamBoundary.tsx';
Expand Down
2 changes: 2 additions & 0 deletions packages/preact-browser/source/server/components/HTML.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
HTMLPlaceholderContent,
HTMLPlaceholderAsyncAssets,
HTMLPlaceholderEntryAssets,
HTMLPlaceholderPreloadAssets,
HTMLPlaceholderSerializations,
} from './HTMLPlaceholder.tsx';

Expand Down Expand Up @@ -89,6 +90,7 @@ export function HTMLBody({
</div>

<HTMLPlaceholderAsyncAssets />
<HTMLPlaceholderPreloadAssets />
</>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export function HTMLPlaceholderAsyncAssets() {
// @ts-expect-error Just used as a placeholder
return <browser-response-placeholder-async-assets />;
}

export function HTMLPlaceholderPreloadAssets() {
// @ts-expect-error Just used as a placeholder
return <browser-response-placeholder-preload-assets />;
}
23 changes: 23 additions & 0 deletions packages/preact-browser/source/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {
import {HTML} from './components/HTML.tsx';
import {Serialization} from './components/Serialization.tsx';
import {ScriptAssets} from './components/ScriptAssets.tsx';
import {ScriptAssetsPreload} from './components/ScriptAssetsPreload.tsx';
import {StyleAssets} from './components/StyleAssets.tsx';
import {StyleAssetsPreload} from './components/StyleAssetsPreload.tsx';

export type RenderAppValue =
| string
Expand Down Expand Up @@ -424,6 +426,27 @@ async function renderHTMLChunk(

break;
}
case 'preload-assets': {
if (assets == null) {
throw new Error(
`Found the preload-assets placeholder, but no assets were provided while rendering`,
);
}

const preloadAssets = assets.modules(
browser.assets.get({timing: 'load'}),
{request: browser.request},
);

replacement = renderToString(
<>
<ScriptAssetsPreload scripts={preloadAssets.scripts} />
<StyleAssetsPreload styles={preloadAssets.styles} />
</>,
);

break;
}
// Add more cases for other placeholder types if needed
default: {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/integrations/htmx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('htmx', () => {
import '@quilted/quilt/globals';
import {parseHTMXRequestHeaders, HTMXResponse} from '@quilted/htmx';
import {RequestRouter} from '@quilted/quilt/request-router';
import {renderToHTMLResponse, HTML, HTMLPlaceholderEntryAssets } from '@quilted/quilt/server';
import {renderToHTMLResponse, HTML, HTMLPlaceholderEntryAssets} from '@quilted/quilt/server';
import {BrowserAssets} from 'quilt:module/assets';
const router = new RequestRouter();
Expand Down

0 comments on commit 4df5ab6

Please sign in to comment.