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

Change style-loader to something SSR-ready #3087

Merged
merged 10 commits into from
May 20, 2021
Merged

Conversation

jimbo
Copy link
Contributor

@jimbo jimbo commented Mar 26, 2021

Description

In the course of removing (or guarding) window and other browser-only global references from the codebase, it became clear that a few references existed in dependency code. One such dependency is style-loader, a standard Webpack loader that inserts CSS into <style> elements. The maintainers of style-loader appear to be aware of the issue, but have closed it as won't-fix, so we need to replace it.

The closest server-compatible loader I can find is isomorphic-style-loader; while it has some minor flaws (hard dependency on react) and doesn't appear to be actively maintained anymore, it is MIT licensed, so we should be able to incorporate it with proper attribution. I've tried to do so in this PR.

The largest change proposed here is a switch from mergeClasses to a new hook (useStyle) with context, which allows style accumulation and insertion to work differently between browser and server.

Related Issue

Merges into #2991

Acceptance

Verification Stakeholders

Specification

Verification Steps

  1. Verify the app builds without errors (yarn watch:venia).
  2. Verify there are no console errors.
  3. Verify the site doesn't render differently from develop.

Screenshots / Screen Captures (if appropriate)

Checklist

  • I have added tests to cover my changes, if necessary.
  • I have added translations for new strings, if necessary.
  • I have updated the documentation accordingly, if necessary.

@PWAStudioBot
Copy link
Contributor

PWAStudioBot commented Mar 26, 2021

Fails
🚫

node` failed.

🚫

Unable to build scaffolded project.

yarn build

within a scaffolded project directory failed.
Learn more about Scaffolding at https://magento.github.io/pwa-studio/pwa-buildpack/scaffolding/.

🚫

Unit tests in the following files did not pass 😔. All tests must pass before this PR can be merged

  • packages/peregrine/lib/targets/__tests__/peregrine-targets.spec.js
  • packages/venia-concept/src/__tests__/index.spec.js
🚫

No linked issue found. Please link a relevant open issue by adding the text "closes #<issue_number>" or "closes JIRA-<issue_number>" in your PR.

Messages
📖 DangerCI Failures related to missing labels/description/linked issues/etc will persist until the next push or next nightly build run (assuming they are fixed).
📖

Access a deployed version of this PR here. Make sure to wait for the "pwa-pull-request-deploy" job to complete.

Log

ERROR ON TASK: unitTests


ERROR ON TASK: scaffoldingSucceeds


Error:  Danger had errors running. See message(s) above for more details.
danger-results://tmp/danger-results.json

Generated by 🚫 dangerJS against d086c3b

@jimbo jimbo added the version: Major This changeset includes incompatible API changes and its release necessitates a Major version bump. label Mar 26, 2021
@jimbo jimbo marked this pull request as ready for review May 4, 2021 16:35
Copy link
Contributor

@tjwiebell tjwiebell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spotted some hard-coded values that need resolved, and a few TODO's, but overall the re-factor to useStyle looks good. We've picked up additional mergeClasses calls from mainline that'll need updated, but probably best to wait until this is ready for delivery, and update once more.

const { BrowserPersistence } = Util;
const apiBase = new URL('/graphql', location.origin).toString();
const origin = 'https://magento-venia-concept-e1r4b.local.pwadev:8048';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hard-coded value likely only works on your local, this will need some SSR treatment as well since location.origin will not be available. Pretty sure you won't have access to useLocation outside of the router context either, so some sort of communication from UPWARD would need to inform Apollo of its origin. React Router docs also seem to indicate the need to use the stateless StaticRouter instead of BrowserRouter, should that be in this scope?

Copy link
Contributor Author

@jimbo jimbo May 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I should have reworked this. The server will have to provide the origin when it executes the script, so we'll probably want to pull parts of src/index.js into a separate function that takes origin as an argument. I'll make that change tomorrow.

As for StaticRouter, yes, it'll have to be done later. There's a fair bit more work to be done to get the server bundle to actually render something, but this work is just to let it run without throwing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tjwiebell Okay. I've refactored Adapter to follow our talon-component pattern, slimmed down the index.js entrypoint, and set origin to MAGENTO_BACKEND_URL on the server for now. When we dig into the real SSR work and have an express handler, we can find a way to retrieve origin from the HTTP request and get it to our script.

});

// remove HtmlWebpackPlugin
serverConfig.plugins.pop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this position guaranteed? Seems a little fragile, but comfortable with this if we control both sides.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No; it's not guaranteed, and it is fragile. I've updated the code to filter by plugin name rather than index.

Copy link
Contributor

@tjwiebell tjwiebell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to merge into the source branch, love the adapter abstraction and how clean the component looks with prop collections. There are some failures related to scaffolding, but I'm comfortable merging this and we'll get everything passing in #2991.

@tjwiebell tjwiebell merged commit bc6d773 into jimbo/window May 20, 2021
@michaelyu0123 michaelyu0123 deleted the jimbo/ssr-readiness branch February 1, 2022 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:pagebuilder pkg:peregrine pkg:pwa-buildpack pkg:venia-concept pkg:venia-ui Progress: done version: Major This changeset includes incompatible API changes and its release necessitates a Major version bump.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants