Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable onChange and route() during SSR #331

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class Router extends Component {
this.state = {
url: props.url || getCurrentUrl()
};

initEventListeners();
}

Expand Down Expand Up @@ -179,7 +179,12 @@ class Router extends Component {
}

componentWillMount() {
ROUTERS.push(this);
// preact-render-to-string does not initialize the "next state" field for components.
// We can use this to detect a static rendering environment and disable subscriptions.
this._ssr = '_nextState' in this || '__s' in this;
developit marked this conversation as resolved.
Show resolved Hide resolved
if (!this._ssr) {
ROUTERS.push(this);
}
this.updating = true;
}

Expand All @@ -194,7 +199,7 @@ class Router extends Component {

componentWillUnmount() {
if (typeof this.unlisten==='function') this.unlisten();
ROUTERS.splice(ROUTERS.indexOf(this), 1);
ROUTERS.splice(ROUTERS.indexOf(this) >>> 0, 1);
}

componentWillUpdate() {
Expand Down Expand Up @@ -230,7 +235,7 @@ class Router extends Component {
let current = active[0] || null;

let previous = this.previousUrl;
if (url!==previous) {
if (!this._ssr && url!==previous) {
this.previousUrl = url;
if (typeof onChange==='function') {
onChange({
Expand Down