Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Jan 15, 2024
1 parent d063218 commit ccf9972
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/utils/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ export type GenericReturnType =
| null[];

// this is workaround for `if` case, where we don't have stable root, and to remove it properly we need to look into last rendered part
export const relatedRoots: WeakMap<
DocumentFragment | HTMLElement,
GenericReturnType
> = new WeakMap();
export const relatedRoots: WeakMap<DocumentFragment, GenericReturnType> =
new WeakMap();

function renderNode(parent: Node, target: Node, placeholder: Node | Comment) {
if (import.meta.env.DEV) {
Expand Down Expand Up @@ -177,6 +175,10 @@ async function destroyNode(node: Node) {
if (parent !== null) {
parent.removeChild(node);
} else {
if (import.meta.env.SSR) {
console.warn(`Node is not in DOM`, node.nodeType, node.nodeName);
return;
}
throw new Error(`Node is not in DOM`);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/rehydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function lastItemInStack(target: 'text' | 'node' | 'comment') {
) as HTMLElement;
if (maybeNextNode) {
// remove data attribute
maybeNextNode.dataset.nodeId = '';
maybeNextNode.removeAttribute('data-node-id');
// remove from stack
const indexInStack = withRehydrationStack.indexOf(maybeNextNode);
if (indexInStack > -1) {
Expand Down
16 changes: 8 additions & 8 deletions src/utils/ssr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderComponent, type ComponentReturnType } from '@/utils/component';
import { renderComponent, runDestructors, type ComponentReturnType } from '@/utils/component';
import { setDocument, getDocument } from './dom-api';
import { resetNodeCounter } from '@/utils/dom';
import { getRoot, resetNodeCounter, resetRoot } from '@/utils/dom';

type EnvironmentParams = {
url: string;
Expand All @@ -24,7 +24,6 @@ export async function render(
) {
const { Window } = await import('happy-dom');
const win = new Window({ url: params.url });
const originalDocument = getDocument();
const doc = win.document;
setDocument(doc as unknown as Document);

Expand All @@ -40,10 +39,11 @@ export async function render(

const html = rootNode.innerHTML;

// @ts-expect-error
await el.destroy();
resetNodeCounter();

setDocument(originalDocument as unknown as Document);
const oldRoot = getRoot();
if (oldRoot) {
await runDestructors(oldRoot);
resetRoot();
}
win.close();
return html;
}

0 comments on commit ccf9972

Please sign in to comment.