-
Notifications
You must be signed in to change notification settings - Fork 45
Proposal: Implement additional SSR detection and control #176
Proposal: Implement additional SSR detection and control #176
Conversation
Codecov Report
@@ Coverage Diff @@
## master #176 +/- ##
==========================================
+ Coverage 93.39% 93.76% +0.37%
==========================================
Files 16 16
Lines 333 337 +4
Branches 65 65
==========================================
+ Hits 311 316 +5
Misses 13 13
+ Partials 9 8 -1
Continue to review full report at Codecov.
|
88a2bbc
to
1b38623
Compare
// XHR/fetch requests do not have `text/html` in the Accept headers | ||
if (!ctx.headers.accept) return false; | ||
if (!ctx.headers.accept.includes('text/html')) return false; | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it help to also test for method === 'GET'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea we should add that check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 👍 Let's do this in another PR though so the code + tests don't distract from the core issue in this proposal.
1b38623
to
6fba115
Compare
6fba115
to
c034502
Compare
Just yesterday, I was writing an analysis of various methods that had been proposed here: #116 (comment) This implements one of the recommendations. @slonoed's use case can potentially be better supported out-of-box via the other recommendation With regards to this proposal, I think the injected service should be a function that can early return before the base algorithm. Replacing it altogether via enhance requires adding a lot of composition boilerplate for the most common case, and if said boilerplate is not implemented, you lose default semantics and might potentially break unrelated scenarios. i.e. // do something like
app.register(SSRBlacklistToken, () => ['/path/with/no/ssr'])
// instead of
const SSRDeciderEnhancer = ssrDecider =>
createPlugin({
provides: () => ctx => {
return ssrDecider(ctx) && !ctx.path.match(/paths|with|no|ssr/);
},
});
app.enhance(SSRDeciderToken, SSRDeciderEnhancer); |
I did not see #116, but that is a very nice analysis. I think that this falls under the I think that if we couple this, along side extension checking in the default SSRDecider plugin, that will give us the best of both worlds. A basic extension check for most cases, and also enhancing for power users. (I'm happy to add extension checking to this PR, or we can do it in another PR) |
Yeah, interesting that we came up with similar solutions independently :)
Hmm, that's a good point. Something that just occurred to me: why do we need to return a plugin? Can we do: const SSRDecider = decide =>
ctx => decide(ctx) && !ctx.path.match(/paths|with|no|ssr/); Also, can we simplify the base check? const SSRDecider = isSSRSuggested =>
ctx => isSSRSuggested && !ctx.path.match(/paths|with|no|ssr/);
// or
const SSRDecider = (ctx, isSSRSuggested) =>
isSSRSuggested && !ctx.path.match(/paths|with|no|ssr/); |
Yes, I think I just like the idea that everything is a plugin, but we should be able to simplify it. We may also want to support the plugin case for deps though, but documenting the simple case is better for users. I will try to make those updates, along with test cases. |
262856a
to
b8201a0
Compare
New updates, simplified documented power-user enhancer. The new syntax is pretty clean: app.enhance(SSRDeciderToken, decide => ctx =>
decide(ctx) && !ctx.path.match(/ignore-ssr-route/)
); Also added additional extensions which should solve the majority of cases. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
!merge |
This implements two SSR detection features.
Additional non-SSR whitelisted extensions
Adds the following extensions: (js|gif|jpg|png|pdf|json)
SSRDeciderToken for enhancing SSR logic
Exports our current SSR logic as a new token, SSRDeciderToken, which can be enhanced through the DI system. This is a change which keeps our API surface fairly small, isn't difficult to support, and allows folks to control server rendering while we iterate on a future solution.
For more discussion and a breakdown of supported URIs see: #116 (comment)
Fixes #45
Fixes #116