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

Bugfix: fix route flashing for routes that render fragments #504

Merged
merged 4 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/odd-singers-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"preact-iso": patch
---

Bugfix: fix route flashing for routes that render fragments
13 changes: 6 additions & 7 deletions packages/preact-iso/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,18 @@ export function Router(props) {
const prevChildren = useRef();
const pending = useRef();

let reverse = false;
if (url !== cur.current.url) {
reverse = true;
pending.current = null;
prev.current = cur.current;
prevChildren.current = curChildren.current;
// old <Committer> uses the pending promise ref to know whether to render
prevChildren.current.props.pending = pending;
cur.current = loc;

// Hi! Wondering what this horrid line is for? That's totally reasonable, it is gross.
// It prevents the old route from being remounted because it got shifted in the children Array.
// @ts-ignore-next
if (this.__v && this.__v.__k) this.__v.__k.reverse();
}

curChildren.current = useMemo(() => {
Expand Down Expand Up @@ -120,11 +123,7 @@ export function Router(props) {
} else commit();
}, [url]);

// Hi! Wondering what this horrid line is for? That's totally reasonable, it is gross.
// It prevents the old route from being remounted because it got shifted in the children Array.
if (reverse && this.__v && this.__v.__k) this.__v.__k.reverse();

return [curChildren.current, prevChildren.current];
return [prevChildren.current, curChildren.current];
}

function Committer({ pending, children }) {
Expand Down
3 changes: 2 additions & 1 deletion packages/preact-iso/test/router.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { jest, describe, it, beforeEach, expect } from '@jest/globals';
import { h, html, render } from 'htm/preact';
import { h, render } from 'preact';
import { html } from 'htm/preact';
import { LocationProvider, Router, useLocation } from '../router.js';
import lazy, { ErrorBoundary } from '../lazy.js';

Expand Down