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

ssr example extended to highlight hydration #190

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 5 additions & 1 deletion ssr/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# SSR

This example shows how you can render and hydrate a simple Riot.js application using `@riotjs/ssr`.
This example shows how you can render and hydrate a simple Riot.js application using `@riotjs/ssr` and `@riotjs/hydrate`.

When loaded in the browser, the app is first statically rendered from the server, clicking on the button does not work (clicking on a navigation link results in a new page load).

After a few seconds, the app is hydrated and fully working.

## Run locally

Expand Down
9 changes: 9 additions & 0 deletions ssr/app/app.riot
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<route each={page in props.pages} path={page.path}>
<main is={page.component}/>
</route>

<div>
<button onclick="{ onClick }">Click me!</button>
</div>
</router>

<script>
Expand All @@ -33,6 +37,7 @@
},
// the isServer property is automatically injected by @riotjs/ssr
onBeforeMount({isServer, initialRoute}) {
console.log('onBeforeMount, SSR mode: ' + (isServer ? '<YES>' : '<NO>'))
// create a stream on all routes
this.anyRouteStream = route('(.*)')
// create a stream to check the riot-router state
Expand Down Expand Up @@ -68,6 +73,10 @@
onBeforeUnmount() {
this.routerStateStream.end()
this.anyRouteStream.end()
},

onClick() {
alert('hello!')
}
}
</script>
Expand Down
6 changes: 5 additions & 1 deletion ssr/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ import '@riotjs/hot-reload'
import hydrate from '@riotjs/hydrate'
import App from './app.riot'

hydrate(App)(document.querySelector('app'), window.__INITIAL_STATE__)
console.log('APP loaded and rendered statically, hydrating in a few seconds...')
setTimeout(() => {
hydrate(App)(document.querySelector('app'), window.__INITIAL_STATE__)
console.log('APP hydrated')
}, 5000)
8 changes: 7 additions & 1 deletion ssr/app/pages/about.riot
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<about>
<h1>Something about me</h1>
<h1>Something about me (in blue)</h1>
<p>Here I describe who I am and what I do</p>

<style>
h1 {
color: blue;
}
</style>
</about>
8 changes: 7 additions & 1 deletion ssr/app/pages/home.riot
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<home>
<h1>I am the home page</h1>
<h1>I am the home page (in green)</h1>
<p>Welcome</p>

<style>
h1 {
color: darkgreen;
}
</style>
</home>