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

fix: document body undefined #518

Merged
merged 7 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/waku/src/lib/handlers/handler-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ export function createHandler<
config,
pathname: req.url.pathname,
searchParams: req.url.searchParams,
htmlHead: `${config.htmlHead}
<script src="${config.basePath}${config.srcDir}/${config.mainJs}" async type="module"></script>`,
htmlHead: config.htmlHead,
renderRscForHtml: async (input, searchParams) => {
const [readable, nextCtx] = await renderRscWithWorker({
input,
Expand Down
6 changes: 3 additions & 3 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export function rscIndexPlugin(config: {
<html>
<head>
${config.htmlHead}
<script src="${config.basePath}${config.srcDir}/${config.mainJs}" async type="module"></script>
</head>
<body>
<script src="${config.basePath}${config.srcDir}/${config.mainJs}" async type="module"></script>
</body>
</html>
`;
Expand All @@ -26,9 +26,9 @@ ${config.htmlHead}
configureServer(server) {
return () => {
server.middlewares.use((req, res) => {
res.statusCode = 200;
res.setHeader('content-type', 'text/html; charset=utf-8');
server.transformIndexHtml(req.url || '', html).then((content) => {
res.statusCode = 200;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to the PR, but it's a minor change, since we used to set the status 200 even though the .then might never work and the promise might reject.

So we just bring it when we're sure the promise is getting resolved.

res.setHeader('content-type', 'text/html; charset=utf-8');
res.end(content);
});
});
Expand Down
13 changes: 13 additions & 0 deletions packages/waku/src/lib/renderers/html-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Promise.resolve(new Response(new ReadableStream({

const injectRscPayload = (
readable: ReadableStream,
config: ResolvedConfig,
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
urlForFakeFetch: string,
) => {
const chunks: Uint8Array[] = [];
Expand Down Expand Up @@ -93,6 +94,7 @@ globalThis.__WAKU_PREFETCHED__ = {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
let headSent = false;
let mainJsSent = false;
let data = '';
let scriptsClosed = false;
const sendScripts = (
Expand All @@ -102,6 +104,16 @@ globalThis.__WAKU_PREFETCHED__ = {
if (scriptsClosed) {
return;
}
if (!mainJsSent) {
// enqueue `mainJs` first in the body to avoid the document.body undefined error, before we used to inject it in the head which sometimes made it load before the document.body was loaded
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My oversight, this should be DEV only, right?

npm run examples:prd:04_promise

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should in general send the mainJs one time, weird error, do you know why that happens?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the fix #528

main.tsx doesn't exist on PRD, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, what exists then if that does not exist? because as far as I checked the website, there was an alternative. And we need that to be in the body too, so the behaviour is normalized between two environments.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's bundled by vite.

it sounds like you just miss a point. do you understand why we get 404?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the error that this PR fixed was reproduced with async too, so I don't think that can always be true and we need to be sure about that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that the issue this PR fixed is for HMR in dev mode. I never see such an issue in PRD.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it fixed the HMR issue after the refresh in DEV, but I was just concerned how it'd be handled in PRD. That's it. sorry for the confusion.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, my question for you is how Vite+React does it. My guess is that all scripts are in <head>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, they're in the head! because the body is always sent in the initial template (index.html)

controller.enqueue(
encoder.encode(
`<script src="${config.basePath}${config.srcDir}/${config.mainJs}" async type="module"></script>`,
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
),
);
mainJsSent = true;
}

const scripts = chunks.splice(0).map(
(chunk) =>
`
Expand Down Expand Up @@ -337,6 +349,7 @@ export const renderHtml = async (
);
const [copied, interleave] = injectRscPayload(
stream,
config,
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
config.basePath + config.rscPath + '/' + encodeInput(ssrConfig.input),
);
const elements: Promise<Record<string, ReactNode>> = createFromReadableStream(
Expand Down
Loading