-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat: __webpack_nonce__ support in SSR + Browser contexts #1022
feat: __webpack_nonce__ support in SSR + Browser contexts #1022
Conversation
src/models/ServerStyleSheet.js
Outdated
html += '>' | ||
|
||
/* eslint-disable */ | ||
const css = Object.keys(this.components).reduce((styles, key) => { |
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.
was able to remove an array iteration here
const css = format(sheet.getStyleTags()) | ||
|
||
expect(html).toEqual('<h1 class="sc-a b" data-reactroot="" data-reactid="1" data-react-checksum="197727696">Hello SSR!</h1>') | ||
expect(css).toEqual(format(` |
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.
@mxstbr would you guys be open to switching these to snapshot tests? I think it might simplify things a bit.
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.
Let's open an issue/another PR for that?
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.
Will do. I added some new tests for previously uncovered code which use snaps but I'll do a separate PR to convert existing tests where it makes sense.
Not really sure why rollup is failing. The error seems to indicate the JSX isn't being transformed? |
4951223
to
bfc2868
Compare
.map(key => this.components[key].css) | ||
.join('') | ||
const attrs = [ | ||
'type="text/css"', |
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.
I think style elements don't actually need this anymore.
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.
Let's still keep it for completeness sake. We don't control what kind of HTML type this renders in. A bit redundant in 2017, but better than having people nitpick it.
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, @jollife does this fix your issue?
const css = format(sheet.getStyleTags()) | ||
|
||
expect(html).toEqual('<h1 class="sc-a b" data-reactroot="" data-reactid="1" data-react-checksum="197727696">Hello SSR!</h1>') | ||
expect(css).toEqual(format(` |
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.
Let's open an issue/another PR for that?
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.
Some minor nits.
But another small thing; We'll have to check that using the global
keyword is fine when it runs through rollup, and if it does, we have to check whether it causes any issues when it's not bundled using webpack. i.e. do we have to add a typeof
check as well instead. Any best practices that we can refer to instead of blindly testing all these cases, as I'm a little unsure about some of them?
src/models/ServerStyleSheet.js
Outdated
|
||
return `<style type="text/css" ${namesAttr} ${localAttr}>\n${css}\n</style>` | ||
return `<style ${attrs.join(' ')}>${css}</style>` | ||
} | ||
|
||
toReactElement(key: string) { |
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.
The same props will need to be added to this method down here
src/models/ServerStyleSheet.js
Outdated
`${LOCAL_ATTR}="${this.isLocal ? 'true' : 'false'}"`, | ||
] | ||
|
||
/* eslint-disable no-underscore-dangle */ |
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.
Can you instead just disable this globally for this file. Shouldn't be an issue.
Also what does the disabled eslint down below prevent from throwing an error? Seems like a bad idea to just disable everything without a comment.
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.
It was complaining about using a comma and I guess there's an eslint rule that all arrow functions have to be one-liners without curlies?
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.
Ok, in that case, just apply the suggestion below and it should become unnecessary ;)
src/models/ServerStyleSheet.js
Outdated
|
||
/* eslint-disable */ | ||
const css = Object.keys(this.components).reduce((styles, key) => { | ||
return (styles += this.components[key].css), styles |
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.
you can just return styles + this.components[key].css
and even turn it into an arrow functions. Gets rid of the mutation and is easier to read.
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.
I actually ended up making this into a reusable function since the same thing happens in toReactElement
Looking into the rollup thing - might need to add this plugin: https://www.npmjs.com/package/rollup-plugin-node-globals |
Okay I found a way around it, using this seems to work without modifying rollup: if (typeof __webpack_nonce__ !== 'undefined') {
attrs.push(`nonce="${__webpack_nonce__}"`)
} Basically just letting it implicitly refer to global scope |
bfc2868
to
97c7cc1
Compare
@philpl addressed your comments |
|
00a9e54
to
cfdd946
Compare
Doesn't look like there were any output verification tests.
cfdd946
to
1eea50e
Compare
|
||
expect(elements).toHaveLength(2); | ||
|
||
expect(elements[0].props).toMatchSnapshot() |
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.
I have yet another small nit here, sorry 😉
This test is basically the same as the above one, but we care about the nonce being present and set to
the global above. So we should probably just check for this directly instead of duplicating the above snapshots and having to check manually 😄
@probablyup Awesome work! I've only got one small nit left (see above comment) |
This might just be me, but I always test the base state and modifications
separately. Easier to track individual regressions.
…On Sat, Jul 22, 2017 at 9:22 PM Phil Plückthun ***@***.***> wrote:
@probablyup <https://github.com/probablyup> Awesome work! I've only got
one small nit left (see above comment)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1022 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAiy1k-e1amAGFgXgtH-cEM6ZJlVrjEJks5sQqBcgaJpZM4OgOUZ>
.
|
@probablyup that's a good default testing strategy, but in this case we don't care about anything but the nonce there. We're only testing:
Nothing about the rest of the output. The two concerns don't overlap :) |
@philpl done |
@mxstbr sorry for Not giving any feedback. But my github handle is unfortunately Not @jollife. Will investigate this Next week! |
Oh no worries, please try it out and let us know if it works! |
Hi guys, I tested this together with @stnwk and we found out the following:
Clarification: You're looking for It's getting replaced as a simple string. So, the correct implementation would be the following:
We still have a problem with |
For the In injectGlobal.js#L14 we are calling the function In the case of being on the client-side, e.g. using In this function we check wether the I'm not sure wether the check for the Would also suggest to add a test for this specific case using Cheers :) |
@probablyup are you open to creating a new PR with the above changes? :) Fixing injectGlobal should be corrected by moving the nonce setAttribute logic out of the surrounding if-name clause, right? |
Ironically I had it that way for a while haha. I'm not sure how we are
going to unit test a magic variable though since it's not actually in any
scope.
…On Mon, Jul 24, 2017 at 8:19 AM Phil Plückthun ***@***.***> wrote:
@probablyup <https://github.com/probablyup> are you open to creating a
new PR with the above changes? :)
Fixing injectGlobal should be corrected by moving the nonce setAttribute
logic out of the surrounding if-name clause, right?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1022 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAiy1lNva7-Js9FHqtbBl3cWqT7igni1ks5sRIvfgaJpZM4OgOUZ>
.
|
Maybe taking a look at how webpack tests magic variables helps? @probablyup |
Yeah or making a util that returns it and mocking the util. That's probably
the most straightforward.
…On Mon, Jul 24, 2017 at 9:16 AM Stefan ***@***.***> wrote:
Maybe taking a look at how webpack tests magic variables
<https://github.com/webpack/webpack/blob/master/test/cases/nonce/set-nonce/index.js>helps?
@probablyup <https://github.com/probablyup>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1022 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAiy1id_asthBu_8AdCoNyihKxCDJNH-ks5sRJkugaJpZM4OgOUZ>
.
|
Closes #887