Comments (3)
What about setting the data in the HTML markup, like Redux?
https://redux.js.org/recipes/server-rendering#inject-initial-component-html-and-state
You could then have your resolve
function check if the initial data exists and only fire off a request if it doesn't. The code below is pretty generic, but hopefully paints a clear picture.
I think that there are strategies for including components in the server rendered markup, but I think that including that isn't really worth the hassle.
function renderHandler(req, res) {
const router = createRouter(reusable, routes, {
history: { location: req.url }
});
router.once(({ response }) => {
const Router = createRouterComponent(router);
const markup = renderToString(
<Router>
<App />
</Router>
);
const html = insertMarkup(markup, response);
res.send(html);
});
}
function insertMarkup(markup, response) {
return `<!doctype html>
<html>
<body>
<div id="root">${markup}</div>
<script src="/static/js/bundle.js"></script>
<script>
window.__INITIAL_DATA__ = ${JSON.stringify(response.data)};
</script>
</body>
</html>`;
}
from curi.
Yes, I thought of something like that. For example, I could inject initialData
into external
, then make some wrapper for resolvers which would check if data is already there, return it and then reset for subsequent transitions on client, so that other routes do not return other routes' data. I just hoped that it's possible to solve this problem at library level somehow. Not sure how though. Anyway, this probably needs to be added to server side rendering guide as a recipe. What do you think? I would try to write it once I figure out what to do myself))
from curi.
It would definitely be helpful to have this laid out for others.
I think that I would personally set up a local API that initializes itself with the data set by the server and caches new data after API results. I believe that data stores like Apollo will actually do this for you (if you've jumped on the GraphQL train), but you can also roll your own.
// api.js
const data = window.__INITIAL_DATA__
? window.__INITIAL_DATA__
: {
products: []
};
const api = {
product(id) {
const cached = data.products.find(p => p.id === id);
if (cached) {
return Promise.resolve(cached);
} else {
return fetch(`/api/product/${id}`)
.then(response => response.json())
.then(result => {
// cache result
data.products.push(result);
return result;
});
}
}
}
export default api;
// routes.js
import api from "./api";
export default prepareRoutes([
// ...
{
name: "Product",
path: "product/:id",
resolve({ params }) {
return api.product(params.id);
}
]);
from curi.
Related Issues (20)
- [React] CSS not loading when using redirectTo HOT 35
- React Packages v2 Plans HOT 2
- Parent Resolve Correlation to Child Route HOT 3
- You should be on Awesome x: HOT 1
- Offscreen positioning possibly bad for RTL languages HOT 2
- 'process' is not defined HOT 6
- SyntaxError: Unexpected token < HOT 5
- .html > .svelte HOT 1
- useRouter not updating state without useResponse HOT 2
- Navigation not working if using svelte beyond v3.6.4 HOT 6
- It appears that when I change the url the entire app is re-rendered HOT 1
- Customizing styles of `useNavigationFocus` / ref HOT 2
- How do I change route programmatically HOT 2
- [Feature] Redirect to url HOT 4
- Typescript Type Error for <Link> HOT 3
- Apollo Integration - Prefetching HOT 4
- webiste on different folder structure HOT 5
- @curi/static and svelte HOT 6
- Undefined value for optional route parameter HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from curi.