Skip to content

Commit

Permalink
feat(router): title update on navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Mar 13, 2022
1 parent 37101c4 commit 3f7b3a8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
17 changes: 6 additions & 11 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { normalizeRelativeURLs } from './utils';
import { fromDomNodeToVNode } from '../shared/convert';
import { patch } from '../million/render';
import { OLD_VNODE_FIELD } from '../million/types';
import { morph } from '../morph/morph';
import { getURL } from './utils';

const parser: DOMParser = new DOMParser();
Expand All @@ -22,17 +20,14 @@ export const navigate = async (url: URL, isBack = false, htmlString?: string) =>

const html = parser.parseFromString(newPageHtmlString, 'text/html');
normalizeRelativeURLs(html, url);
console.log(htmlString);

if (!document.head[OLD_VNODE_FIELD]) {
document.head[OLD_VNODE_FIELD] = fromDomNodeToVNode(document.head);
}
if (!document.body[OLD_VNODE_FIELD]) {
document.body[OLD_VNODE_FIELD] = fromDomNodeToVNode(document.body);
const title = html.querySelector('title');
if (title) {
document.title = title.text;
}

patch(document.head, fromDomNodeToVNode(html.head));
patch(document.body, fromDomNodeToVNode(html.body));
morph(html.head, document.head);
morph(html.body, document.body);
};

export const router = (routes?: Record<string, string>) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export const setAttribute = (el: Element, attr: string, base: string | URL) => {
};

export const normalizeRelativeURLs = (el: Element | Document, base: string | URL) => {
const hrefs = el.querySelectorAll(':is([href^="./"], [href^="../"])');
const srcs = el.querySelectorAll(':is([src^="./"], [src^="../"])');
const hrefs = el.querySelectorAll('[href^="./"], [href^="../"]');
const srcs = el.querySelectorAll('[src^="./"], [src^="../"]');
for (let i = 0; i < hrefs.length; i++) {
setAttribute(hrefs[i], 'href', base);
}
Expand Down

0 comments on commit 3f7b3a8

Please sign in to comment.