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

Feature support for async prefetch in inferno-router #1621

Merged
merged 72 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
3b8dc01
initial code
jhsware Dec 16, 2022
886b621
test beginning to work
jhsware Dec 16, 2022
6953829
working implementation with server side prefetch of loaders for SSR +…
jhsware Dec 17, 2022
facdda4
Fixed call to loader on first render
jhsware Dec 18, 2022
296c67e
Fixed tests and added SSR and browser render
jhsware Dec 18, 2022
8f4b69a
Moved SSR render test to inferno-server
jhsware Dec 18, 2022
70b0d3f
Shrink and move
jhsware Dec 19, 2022
66b2dbc
Use async await on server
jhsware Dec 19, 2022
9759f3f
Fix tests
jhsware Dec 19, 2022
58bdfc3
renamed loaderData to initialData
jhsware Dec 19, 2022
7a17f2a
Pass match params to async loader
jhsware Dec 19, 2022
e9e8697
Move first invokation of loader to lifecycle hook where it belongs
jhsware Dec 19, 2022
5177eec
The condition can be solved through recursion for readability and red…
jhsware Dec 21, 2022
02269b2
Some tests working
jhsware Dec 21, 2022
75bba0f
Fixed regression bugs
jhsware Dec 22, 2022
ff37740
Remove moved code
jhsware Dec 22, 2022
b50c465
Improve typing and structure components to be more similar. Added swi…
jhsware Dec 22, 2022
ac30077
Added demo application to test inferno-router with async loading
jhsware Dec 23, 2022
169691e
Added note on possibly false test
jhsware Dec 23, 2022
5d72431
unused props
jhsware Dec 23, 2022
2f8f2f0
Demo: SSR (WIP) with resolving of loaders and using Parcel to transpi…
jhsware Dec 23, 2022
3c822ab
Serve a proper SSR page to browser
jhsware Dec 23, 2022
b031099
SSR and hydration
jhsware Dec 23, 2022
b26fbc8
Moved invocation of loader to Router
jhsware Dec 23, 2022
52fd289
Fixed redirect issue that broke Switch tests
jhsware Dec 23, 2022
6b5bd8d
Notes for investigation of request
jhsware Dec 24, 2022
2156a4c
Demo using params in loader
jhsware Dec 24, 2022
b1d9488
Added test with params
jhsware Dec 24, 2022
9757aa5
First, dumbed down, try at adding request param and AbortController t…
jhsware Dec 26, 2022
25405c4
Add test for abort signal on nav
jhsware Dec 26, 2022
d108767
Fix demo
jhsware Dec 27, 2022
f5fbd2d
Remove duplicate code in inferno-server
jhsware Dec 27, 2022
3f7826b
Simplify inherited code
jhsware Dec 27, 2022
95abb27
Simplify again, Location objects return a href string
jhsware Dec 28, 2022
72e5768
Change deprecated call, cleanup and and typing
jhsware Dec 28, 2022
3c70029
Switch only renders first match so bailing out early
jhsware Dec 28, 2022
46f9ecd
Don't traverse deeper if Route doesn't match
jhsware Dec 28, 2022
f8b6ffc
Removed loader code in Switch since it is handled by Router now
jhsware Dec 28, 2022
65dd88a
Change name to loaderData for individual match
jhsware Dec 28, 2022
b8aff25
Fixed SSR-rendering and added base argument to traverseLoaders to all…
jhsware Dec 28, 2022
71e44a1
Remove unused imports
jhsware Jan 2, 2023
8a3f0e6
Remove unreachable code
jhsware Jan 2, 2023
b439c8d
Unused types
jhsware Jan 2, 2023
ed050fc
Support subclassing Switch
jhsware Jan 2, 2023
c835a8e
Send cancel signal in demo
jhsware Jan 2, 2023
227f1a3
Fix issue where multiple nav clicks with loaders resolving out of ord…
jhsware Jan 3, 2023
fc4f326
Support typing response from loader data/error helpers
jhsware Jan 3, 2023
4ec8736
Use cross-env for Win-compat
jhsware Jan 3, 2023
65181d6
Fix loader signature in AboutPage.tsx to allow calls to be cancelled
jhsware Jan 3, 2023
4973fb2
Fixed linting errors
jhsware Jan 3, 2023
c62d650
Use custom resolver to allow parcel to choose correct bundle when pac…
jhsware Jan 3, 2023
2a7df6b
Don't expose parentIsSwitch in exported function traverseLoaders sinc…
jhsware Jan 3, 2023
073370b
Trigger rerender after promises have been resolved to make sure any p…
jhsware Jan 4, 2023
804f467
Update test to make sure we don't get false passing (I have broken th…
jhsware Jan 4, 2023
a720c10
Missed a Switch check
jhsware Jan 4, 2023
d676724
Remove parcel-resolver-inferno and use alias instead to pick transpil…
jhsware Jan 11, 2023
9eac3f5
Remove need for esModuleInterop and isolatedModules
jhsware Jan 11, 2023
7f025fa
Update README for inferno-router
jhsware Jan 11, 2023
3a0148e
Update docs
jhsware Jan 11, 2023
216b9fc
Added link to demo and react-router loader docs
jhsware Jan 11, 2023
ae36e0a
Call .json() and .text() on fetch result automatically
jhsware Jan 13, 2023
bd01ab0
Rephrase part about missing features vs. react-router@6
jhsware Jan 14, 2023
e5703a7
Clarified which headers aren't exposed
jhsware Jan 14, 2023
3de7d0c
Bump versions
jhsware Feb 21, 2023
f4a7efe
Cleaned up demo code and added clarification
jhsware Feb 21, 2023
a821567
Convert tests to Typescript
jhsware Apr 26, 2023
37ba7ed
Updated package-lock.json
jhsware Apr 26, 2023
54581c7
Store spy in variable to avoid type error and harmonise naming of sai…
jhsware May 13, 2023
261af08
Improve test of passing Location obj to Link
jhsware May 13, 2023
a9f9eaa
Fix linting errors
jhsware May 13, 2023
ccb8dc0
Final fix
jhsware May 13, 2023
e5b0673
Change type of `to` property so it is compatible with tests and parse…
jhsware May 13, 2023
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 demo/inferno-router-demo/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"inferno"
]
}
22 changes: 22 additions & 0 deletions demo/inferno-router-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.DS_Store
.git/

# VS Code specific
.vscode
jsconfig.json
typings/*
typings.json

# Dependency directory for NPM
node_modules

# Built at startup
.env
pwd.txt
lerna-debug.log

dist
distServer
distBrowser
package-lock.json
.parcel-cache
5 changes: 5 additions & 0 deletions demo/inferno-router-demo/.proxyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"/api": {
"target": "http://127.0.0.1:3000/"
}
}
11 changes: 11 additions & 0 deletions demo/inferno-router-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Demo of Inferno-Router

NOTE: Requires Nodejs >=18 (uses `fetch`)

```sh
cd demo/inferno-router-demo
npm i
npm run dev
```

Go to http://127.0.0.1:1234/
53 changes: 53 additions & 0 deletions demo/inferno-router-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "inferno-router-demo",
"version": "8.0.5",
"description": "Influence CMS Demo",
"author": "Sebastian Ware <sebastian@urbantalk.se> (https://github.com/jhsware)",
"license": "SEE LICENSE IN LICENSE",
"source": "src/index.html",
"browserslist": "> 0.5%, last 2 versions, not dead",
"scripts": {
"dev": "npm-run-all -l --parallel dev:*",
"dev:frontend": "cross-env FORCE_COLOR=1 parcel",
"dev:backend": "cross-env FORCE_COLOR=1 ts-node-dev --respawn src/server.ts"
},
"dependencies": {
"inferno": "file:../../packages/inferno",
"inferno-animation": "file:../../packages/inferno-animation",
"inferno-create-element": "file:../../packages/inferno-create-element",
"inferno-hydrate": "file:../../packages/inferno-hydrate",
"inferno-router": "file:../../packages/inferno-router",
"inferno-server": "file:../../packages/inferno-server",
"koa": "^2.14.1",
"koa-json-body": "^5.3.0",
"koa-logger": "^3.2.1",
"koa-mount": "^4.0.0",
"koa-router": "^12.0.0",
"koa-static": "^5.0.0"
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.20.11",
"@parcel/config-default": "^2.8.2",
"@parcel/packager-ts": "^2.8.1",
"@parcel/transformer-babel": "^2.8.1",
"@parcel/transformer-sass": "^2.8.1",
"@parcel/transformer-typescript-types": "^2.8.1",
"@types/node": "^18.13.0",
"babel-plugin-inferno": "^6.6.0",
"cross-env": "^7.0.3",
"jest-environment-jsdom": "^29.4.2",
"npm-run-all": "^4.1.5",
"parcel": "^2.8.2",
"process": "^0.11.10",
"ts-node-dev": "^2.0.0",
"typescript": "^4.9.5"
},
"alias": {
"inferno": "inferno/dist/index.dev.esm.js",
"inferno-animation": "inferno-animation/dist/index.dev.esm.js",
"inferno-create-element": "inferno-create-element/dist/index.dev.esm.js",
"inferno-hydrate": "inferno-hydrate/dist/index.dev.esm.js",
"inferno-router": "inferno-router/dist/index.dev.esm.js",
"inferno-server": "inferno-server/dist/index.dev.esm.js"
}
}
30 changes: 30 additions & 0 deletions demo/inferno-router-demo/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Component } from 'inferno'
import { Route } from 'inferno-router'

/**
* Pages
*/
import StartPage from './pages/StartPage'
import AboutPage from './pages/AboutPage'
import ContentPage from './pages/ContentPage'

/**
* The Application
*/
export class App extends Component {
render(props) {
return props.children;
}
};

export function appFactory () {

return (
<App>
{/* Public Pages */}
<Route exact path={`/`} component={ StartPage } />
<Route exact path={`/about`} component={ AboutPage } loader={AboutPage.fetchData} />
<Route exact path={`/page/:slug`} component={ ContentPage } loader={ContentPage.fetchData} />
</App>
)
}
11 changes: 11 additions & 0 deletions demo/inferno-router-demo/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Inferno Router Demo</title>
<script type="module" src="index.tsx"></script>
</head>
<body>
<div id="app"></div>
</body>
</html>
11 changes: 11 additions & 0 deletions demo/inferno-router-demo/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { render } from 'inferno';
import { BrowserRouter } from 'inferno-router'
import { appFactory } from './App';

declare global {
interface Window {
__initialData__: any
}
}

render(<BrowserRouter initialData={window.__initialData__}>{appFactory()}</BrowserRouter>, document.getElementById('app'))
11 changes: 11 additions & 0 deletions demo/inferno-router-demo/src/indexServer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { hydrate } from 'inferno-hydrate';
import { BrowserRouter } from 'inferno-router'
import { appFactory } from './App';

declare global {
interface Window {
__initialData__: any
}
}

hydrate(<BrowserRouter initialData={window.__initialData__}>{appFactory()}</BrowserRouter>, document.getElementById('app'))
Empty file.
35 changes: 35 additions & 0 deletions demo/inferno-router-demo/src/pages/AboutPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Component } from 'inferno';
import PageTemplate from './PageTemplate';
import { useLoaderData } from 'inferno-router';

import './AboutPage.scss';
import { useLoaderError } from 'inferno-router';

const BACKEND_HOST = 'http://localhost:1234';

export default class AboutPage extends Component {
static async fetchData({ request }) {
const fetchOptions: RequestInit = {
headers: {
Accept: 'application/json',
},
signal: request?.signal,
};

return fetch(new URL('/api/about', BACKEND_HOST), fetchOptions);
}

render(props) {
const data = useLoaderData<{ title: string, body: string}>(props);
const err = useLoaderError<{ message: string }>(props);

return (
<PageTemplate>
<article>
<h1>{data?.title}</h1>
<p>{data?.body || err?.message}</p>
</article>
</PageTemplate>
);
}
}
Empty file.
37 changes: 37 additions & 0 deletions demo/inferno-router-demo/src/pages/ContentPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Component } from 'inferno';
import PageTemplate from './PageTemplate';
import { useLoaderData } from 'inferno-router';

import './AboutPage.scss';
import { useLoaderError } from 'inferno-router';

const BACKEND_HOST = 'http://localhost:1234';

export default class ContentPage extends Component {
static async fetchData({ params, request }) {
const pageSlug = params.id;

const fetchOptions: RequestInit = {
headers: {
Accept: 'application/json',
},
signal: request?.signal
};

return fetch(new URL(`/api/page/${params.slug}`, BACKEND_HOST), fetchOptions);
}

render(props) {
const data = useLoaderData<{ title: string, body: string}>(props);
const err = useLoaderError<{ message: string }>(props);

return (
<PageTemplate>
<article>
<h1>{data?.title}</h1>
<p>{data?.body || err?.message}</p>
</article>
</PageTemplate>
);
}
}
48 changes: 48 additions & 0 deletions demo/inferno-router-demo/src/pages/PageTemplate.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
:root {
--rowGap: 2rem;
}

.page {
display: flex;
flex-flow: column nowrap;
min-height: 100vh;

h1, h2 {
margin-bottom: 1.5em;
}

header {
flex-grow: 0;
& > nav > ul {
display: flex;
flex-flow: row wrap;
gap: var(--rowGap);
align-items: center;
justify-content: center;
padding: 0;

& > li {
list-style: none;
}
}
}
main {
flex-grow: 1;
display: flex;
flex-flow: column;
align-items: center;
justify-content: flex-start;
text-align: center;

& > * {
max-width: 50rem
}

& > .body {
width: 100%;
}
}
footer {
flex-grow: 0
}
}
25 changes: 25 additions & 0 deletions demo/inferno-router-demo/src/pages/PageTemplate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Link } from 'inferno-router'

import './PageTemplate.scss'

export default function PageTemplate({ id = undefined, children }) {
return (
<div id={id} className="page">
<header>
<nav>
<ul>
<li><Link to="/">Start</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/page/one">Page One</Link></li>
<li><Link to="/page/two">Page Two</Link></li>
</ul>
</nav>
</header>
<main>{children}</main>
<footer>
<p>Page Footer</p>
</footer>
</div>
)

}
Empty file.
25 changes: 25 additions & 0 deletions demo/inferno-router-demo/src/pages/StartPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Component } from 'inferno'
import PageTemplate from './PageTemplate'
import './StartPage.scss'

interface IProps {
fetchData: any;
}

export default class StartPage extends Component<IProps> {
static async fetchData({ match }) {
const pageSlug = match.params.id
return [];
}

render() {
return (
<PageTemplate>
<article>
<h1>Start Page</h1>
<p>Some content</p>
</article>
</PageTemplate>
)
}
}
Loading