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
Show file tree
Hide file tree
Changes from all 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);
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
22 changes: 15 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import assertCloneOf from '../test_helpers/assert-clone-of';

chai.use(assertCloneOf);

function createBrowserRouter(props) {
const router = new Router(props);
router.__s = router.state || {}; // _nextState
router.__d = true; // _dirty
router.componentWillMount();
return router;
}

describe('preact-router', () => {
it('should export Router, Link and route', () => {
expect(Router).to.be.a('function');
Expand All @@ -13,7 +21,7 @@ describe('preact-router', () => {

describe('Router', () => {
it('should filter children based on URL', () => {
let router = new Router({});
let router = createBrowserRouter({});
let children = [
<foo path="/" />,
<foo path="/foo" />,
Expand All @@ -34,7 +42,7 @@ describe('preact-router', () => {
});

it('should support nested parameterized routes', () => {
let router = new Router({});
let router = createBrowserRouter({});
let children = [
<foo path="/foo" />,
<foo path="/foo/:bar" />,
Expand All @@ -55,7 +63,7 @@ describe('preact-router', () => {
});

it('should support default routes', () => {
let router = new Router({});
let router = createBrowserRouter({});
let children = [
<foo default />,
<foo path="/" />,
Expand All @@ -76,7 +84,7 @@ describe('preact-router', () => {
});

it('should support initial route prop', () => {
let router = new Router({ url:'/foo' });
let router = createBrowserRouter({ url:'/foo' });
let children = [
<foo default />,
<foo path="/" />,
Expand All @@ -87,14 +95,14 @@ describe('preact-router', () => {
router.render({ children }, router.state)
).to.be.cloneOf(children[2]);

expect(new Router({})).to.have.deep.property('state.url', location.pathname + (location.search || ''));
expect(createBrowserRouter({})).to.have.deep.property('state.url', location.pathname + (location.search || ''));
});

it('should support custom history', () => {
let push = sinon.spy();
let replace = sinon.spy();
let getCurrentLocation = sinon.spy(() => ({pathname: '/initial'}));
let router = new Router({
let router = createBrowserRouter({
history: { push, replace, getCurrentLocation },
children: [
<index path="/" />,
Expand Down Expand Up @@ -123,7 +131,7 @@ describe('preact-router', () => {
let router;

before( () => {
router = new Router({
router = createBrowserRouter({
url: '/foo',
children: [
<foo path="/" />,
Expand Down