Skip to content

Commit

Permalink
Fix page-router to diff & patch all elements from the newly loaded pa…
Browse files Browse the repository at this point in the history
…ge. Fixes #141
  • Loading branch information
tipiirai committed Jan 15, 2024
1 parent c47713c commit 8daccb1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/nuekit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuekit",
"version": "0.3.0",
"version": "0.3.1",
"description": "The closer-to-standards web framework. Build sites and apps with less effort.",
"homepage": "https://nuejs.org",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions packages/nuekit/src/browser/app-router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

// Router for single-page applications
import { onclick, loadPage, setSelected } from './page-router.js'

const fns = []
Expand Down
41 changes: 26 additions & 15 deletions packages/nuekit/src/browser/page-router.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@

// MPA router

// Router for multi-page applications
const is_browser = typeof window == 'object'

export async function loadPage(path) {
dispatchEvent(new Event('before:route'))

// DOM of the new page
const dom = mkdom(await getHTML(path))

// title
// change title
document.title = $('title', dom)?.textContent

// main / body
const main = $('main')
const main2 = $('main', dom)

if (main && main2) {
main.replaceWith(main2)
} else {
$('body').innerHTML = $('body2', dom).innerHTML
$('body').classList = $('body2', dom).classList
}

// inline CSS
const new_styles = swapStyles($$('style'), $$('style', dom))
new_styles.forEach(style => $('head').appendChild(style))

// body class
$('body').classList = $('body2', dom).classList

// content
for (const query of ['header', 'main', 'footer']) {
const a = $('body >' + query)
const b = $('body2 >' + query, dom)

// update (if changed)
if (a && b) {
if (a.outerHTML != b.outerHTML) a.replaceWith(b)

// remove
} else if (a) {
a.remove()

// add
} else {
const fn = query == 'footer' ? 'append' : 'prepend'
document.body[fn](b)
}
}

// stylesheets
// external CSS
const paths = swapStyles($$('link'), $$('link', dom))

loadCSS(paths, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuekit/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function init({ dist, is_dev, esbuild }) {


// has all latest?
const latest = join(outdir, '.020')
const latest = join(outdir, '.031')
try {
return await fs.stat(latest)

Expand Down

0 comments on commit 8daccb1

Please sign in to comment.