Skip to content

Commit

Permalink
Add useTitle (#311)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Carniato <[email protected]>
  • Loading branch information
lxsmnsyc and ryansolid authored Sep 11, 2024
1 parent f52b19c commit 553935b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
56 changes: 23 additions & 33 deletions packages/dom-expressions/src/client.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,31 @@
import {
Properties,
ChildProperties,
Aliases,
getPropAlias,
SVGNamespace,
DelegatedEvents
} from "./constants";
import {
root,
createComponent,
effect,
memo,
getOwner,
createComponent,
memo,
mergeProps,
root,
sharedConfig,
untrack,
mergeProps
untrack
} from "rxcore";
import reconcileArrays from "./reconcile";
export {
Properties,
ChildProperties,
getPropAlias,
import {
Aliases,
DOMElements,
SVGElements,
ChildProperties,
DelegatedEvents,
Properties,
SVGNamespace,
DelegatedEvents
getPropAlias
} from "./constants";
import reconcileArrays from "./reconcile";
export {
Aliases, ChildProperties, DOMElements, DelegatedEvents, Properties, SVGElements,
SVGNamespace, getPropAlias
} from "./constants";

const $$EVENTS = "_$DX_DELEGATE";

export {
effect,
memo,
untrack,
getOwner,
createComponent,
mergeProps,
voidFn as useAssets,
voidFn as getAssets,
voidFn as Assets,
voidFn as generateHydrationScript,
voidFn as HydrationScript,
voidFn as getRequestEvent
voidFn as Assets, voidFn as HydrationScript, createComponent, effect, voidFn as generateHydrationScript, voidFn as getAssets, getOwner, voidFn as getRequestEvent, memo, mergeProps, untrack, voidFn as useAssets
};

export function render(code, element, init, options = {}) {
Expand Down Expand Up @@ -618,3 +601,10 @@ export const RequestContext = Symbol();
export function innerHTML(parent, content) {
!sharedConfig.context && (parent.innerHTML = content);
}

export function useTitle(source) {
effect(() => {
while (typeof source === 'function') source = source();
document.title = source;
});
}
20 changes: 17 additions & 3 deletions packages/dom-expressions/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ export function renderToString(code, options = {}) {
roots: 0,
nextRoot() {
return this.renderId + "i-" + this.roots++;
}
},
title: '',
};
let html = root(d => {
setTimeout(d);
return resolveSSRNode(escape(code()));
});
sharedConfig.context.noHydrate = true;
serializer.close();
html = injectTitle(sharedConfig.context.title, html);
html = injectAssets(sharedConfig.context.assets, html);
if (scripts.length) html = injectScripts(html, scripts, options.nonce);
return html;
Expand Down Expand Up @@ -214,7 +216,8 @@ export function renderToStream(code, options = {}) {
}
return firstFlushed;
};
}
},
title: '',
};

let html = root(d => {
Expand All @@ -225,6 +228,7 @@ export function renderToStream(code, options = {}) {
if (shellCompleted) return;
sharedConfig.context = context;
context.noHydrate = true;
html = injectTitle(context.title, html);
html = injectAssets(context.assets, html);
if (tasks.length) html = injectScripts(html, tasks, nonce);
buffer.write(html);
Expand Down Expand Up @@ -695,8 +699,18 @@ export function ssrSpread(props, isSVG, skipChildren) {
return result;
}

// client-only APIs
export function useTitle(source) {
// TODO should we resolve this eagerly?
sharedConfig.context.title = source;
}

function injectTitle(title, html) {
const result = resolveSSRNode(title);
// TODO should we put this after the head opening
return result ? html.replace(`</head>`, result + `</head>`) : html;
}

// client-only APIs
export {
notSup as classList,
notSup as style,
Expand Down

0 comments on commit 553935b

Please sign in to comment.