Skip to content

Commit

Permalink
fix: stream import in workers
Browse files Browse the repository at this point in the history
  • Loading branch information
frandiox committed Jan 12, 2022
1 parent cd1c9b7 commit 737944b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/hydrogen/src/entry-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {ServerComponentResponse} from './framework/Hydration/ServerComponentResp
import {ServerComponentRequest} from './framework/Hydration/ServerComponentRequest.server';
import {getCacheControlHeader} from './framework/cache';
import {ServerResponse} from 'http';
import type {PassThrough as PassThroughType} from 'stream';

// @ts-ignore
import * as rscRenderer from '@shopify/hydrogen/vendor/react-server-dom-vite/writer';
Expand Down Expand Up @@ -485,8 +486,7 @@ async function renderToBufferedString(
*/
resolve(await new Response(stream).text());
} else {
const {PassThrough} = await import('stream');
const writer = new PassThrough();
const writer = await createNodeWriter();

const {pipe} = renderToPipeableStream(ReactApp, {
/**
Expand Down Expand Up @@ -627,3 +627,13 @@ function generateHeadTag({title, ...rest}: Record<string, string>) {
return `<head>${headHtml}</head>`;
};
}

async function createNodeWriter() {
// Importing 'stream' directly breaks Vite resolve
// when building for workers, even though this code
// does not run in a worker. Looks like tree-shaking
// kicks in after the import analysis/bundle.
const streamImport = __WORKER__ ? '' : 'stream';
const {PassThrough} = await import(streamImport);
return new PassThrough() as InstanceType<typeof PassThroughType>;
}

0 comments on commit 737944b

Please sign in to comment.