Skip to content

Commit 67aef47

Browse files
authored
Merge pull request #12 from kethinov/1.0.2
1.0.2
2 parents 7ea9baa + b10ebb2 commit 67aef47

File tree

4 files changed

+65
-36
lines changed

4 files changed

+65
-36
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
- Put your changes here...
66

7+
## 1.0.2
8+
9+
- Fixed bug causing custom `res` variables to not be set properly during certain default render method calls.
10+
- Fixed broken Express 5 support.
11+
- Updated dependencies.
12+
713
## 1.0.1
814

915
- Fixed broken postinstall script.

package-lock.json

Lines changed: 36 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"url": "https://github.com/rooseveltframework/single-page-express/graphs/contributors"
99
}
1010
],
11-
"version": "1.0.1",
11+
"version": "1.0.2",
1212
"files": [
1313
"dist"
1414
],
@@ -19,7 +19,7 @@
1919
"node": ">=18.0.0"
2020
},
2121
"dependencies": {
22-
"path-to-regexp": "0.1.12",
22+
"path-to-regexp": "8.2.0",
2323
"path-to-regexp-express4": "npm:path-to-regexp@0.1.12",
2424
"topbar": "3.0.0"
2525
},

single-page-express.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,19 @@ function singlePageExpress (options) {
392392
// app.render implements the express api on the surface, then prescribes some default behavior specific to this module, provides a default method for dom manipulation, and allows for a user to override the default dom manipulation behaviors
393393
app.render = function (template, model, callback) {
394394
model = model || {}
395+
396+
// clear all `this` variables so they do not persist but store local copies for this method invocation's use
397+
const thisTitle = this.title
398+
const thisBeforeRender = this.beforeRender
399+
const thisTarget = this.target
400+
const thisUpdateDelay = this.updateDelay
401+
const thisAfterRender = this.afterRender
402+
this.title = null
403+
this.beforeRender = null
404+
this.target = null
405+
this.updateDelay = null
406+
this.afterRender = null
407+
395408
if (options.renderMethod) {
396409
// execute user-supplied render method if it is provided
397410
options.renderMethod(template, model, callback)
@@ -442,9 +455,9 @@ function singlePageExpress (options) {
442455

443456
if (!err) {
444457
// replace title tag with the new one
445-
if (this.title) { // check if res.title is set
458+
if (thisTitle) { // check if res.title is set
446459
if (document.querySelector('title')) { // check if the title element exists
447-
document.querySelector('title').innerHTML = this.title // replace the page title with the new title from this.title
460+
document.querySelector('title').innerHTML = thisTitle // replace the page title with the new title from res.title
448461
}
449462
} else if (doc.querySelector('title') && document.querySelector('title')) { // otherwise check if a <title> tag exists in the template
450463
document.querySelector('title').innerHTML = doc.querySelector('title').innerHTML // if so, replace the page title with the new title from the <title> tag
@@ -453,12 +466,12 @@ function singlePageExpress (options) {
453466
// call app.beforeEveryRender function if it exists
454467
if (app.beforeEveryRender && typeof app.beforeEveryRender === 'function') app.beforeEveryRender(model) // e.g. document.body.style.opacity = 0
455468

456-
// call this.beforeRender function if it exists
457-
if (this.beforeRender && typeof this.beforeRender === 'function') this.beforeRender(model) // e.g. document.body.style.opacity = 0
469+
// call res.beforeRender function if it exists
470+
if (thisBeforeRender && typeof thisBeforeRender === 'function') thisBeforeRender(model) // e.g. document.body.style.opacity = 0
458471

459472
// update DOM
460473
window.setTimeout(function () {
461-
const target = this.target || app.defaultTarget // check if a target is set
474+
const target = thisTarget || app.defaultTarget // check if a target is set
462475
if (target) {
463476
if (document.querySelector(target)) { // check if the target is a valid DOM element
464477
if (doc.querySelector(target)) { // if the new template has an element with the same id as the target container, then that's the container we're writing to
@@ -484,9 +497,9 @@ function singlePageExpress (options) {
484497
// call app.afterEveryRender function if it exists
485498
if (app.afterEveryRender && typeof app.afterEveryRender === 'function') app.afterEveryRender(model) // e.g. document.body.style.opacity = 1
486499

487-
// call this.afterRender function if it exists
488-
if (this.afterRender && typeof this.afterRender === 'function') this.afterRender(model) // e.g. document.body.style.opacity = 1
489-
}, parseInt(this.updateDelay) || parseInt(app.updateDelay) || 0)
500+
// call res.afterRender function if it exists
501+
if (thisAfterRender && typeof thisAfterRender === 'function') thisAfterRender(model) // e.g. document.body.style.opacity = 1
502+
}, parseInt(thisUpdateDelay) || parseInt(app.updateDelay) || 0)
490503
}
491504
}
492505
}

0 commit comments

Comments
 (0)