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

Don't use global router instance for SSR #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fabien
Copy link

@fabien fabien commented Feb 3, 2021

Hi @lyohaplotinka,

As a follow-up, I was able to get SSR running eventually - the component module signatures for SSR are different, and I was expecting a specific kind in order to hook everything up.

However, I started to notice an unexpected side-effect of global.$$$easyrouteRouter being a router instance.

Since I had implemented some of my own beforeLeave hooks (as shown in the Sandbox.io demo I sent you by email), I saw how SSR requests were suddenly 'stateful': beforeLeave was triggered with the last visited url, even though the server should not actually be aware of this (as it could have been any request, not just mine).

In cases like this, it's always better to opt for a 'shared-none' approach, and solely operate within the boundary of a single request. As such, one should create a new router for every request - perhaps some optimizations can be made within easyroute-core which only recreates the 'history' for SSR (or better yet: doesn't keep anything stateful around).

Please have a look at the code. My solution piggy-backs on the current global.$$$easyrouteRouter but only to get the factory function that creates the router on each request. I'm sure there are better ways, but it's only to give you a general idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant