-
Notifications
You must be signed in to change notification settings - Fork 264
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
Blueprints: Isomorphic DOM - DOMParser, FormData, fetch #379
Comments
Thank you, this would be great! An alternative fix would be to remove reliance on DOMParser entirely, like what this PR did for the |
OK, I see there are two diverging paths, depending on the long-term design of Playground Blueprints.
|
Yes please @eliot-akira ! That sounds absolutely fantastic 💯
That's also my concern with isomorphic DOM. If Playground had some public API that exposed DOM functions that would be different, but right now DOM is an implementation detail and a burden to maintain. DOM might become indispensable one day, and if it does we'll figure it out based on the specific use-case that comes up. For now, I really like the idea of avoiding the complexity associated with a custom |
## What? The goal of this pull request is to remove the use of any browser-specific API such as `DOMParser` from the Blueprints package. This allows all Blueprint steps to run on the browser and server side. ## Why? Currently, the steps `installPlugin` and `installTheme` can only be run with the Playground in the browser, not on server side with `wp-now` and Node.js, because they use the `asDOM` helper function which depends on the `DOMParser` class only available in the browser. Related discussion: - #379 The initial idea was to introduce an "isomorphic DOM" library that exports native DOM API for the browser and [jsdom](https://github.com/jsdom/jsdom) for Node.js. That would have allowed the existing implementation of `installPlugin` and `installTheme` to work as is on server side. However, after weighing the pros and cons, it was decided that it's simpler to maintain if we rewrite these steps to perform their actions without using any DOM operations. ## How? - Rewrite the Blueprint steps `installPlugin` and `installTheme` to use Playground and PHP WASM API. - Remove the `asDOM` helper function. ## Testing Instructions 1. Check out the branch. 2. Run `npx nx test playground-blueprints`
Hi @sejas - I wanted to mention this issue concerning the use of
The original topic of this issue was about "isomorphic DOM", using the With Adam's feedback, the approach was changed to remove the use of all browser-only APIs in Blueprint steps. Mainly that involved rewriting some steps to not use GET/POST request and Apparently that was done for
The remaining step that's yet to be rewritten is While I was working on the PR, I discovered that This workaround requires that all Blueprint steps that use the Also relevant:
|
The only step still using the DOM API is |
…ress-Importer Rewires the importFile step to: * Be named importWxz. The old name continues to work, though. * Use the humanmade/WordPress-Importer for importing content – it is more reliable than the official wordpress-importer and has a convenient PHP API. * Drop WXZ support – it's a maintenance burden and doesn't add clear value. Closes #1183 Closes #379 Closes #1158 Closes #1161
From this merged pull request:
Currently, the following browser globals are found in the
blueprints
package.DOMParser
wordpress-playground/packages/playground/blueprints/src/lib/steps/common.ts
Line 4 in a38fa9a
blueprints/src/lib/steps/install-theme.ts
blueprints/src/lib/steps/install-plugin.ts
FormData
wordpress-playground/packages/playground/blueprints/src/lib/steps/install-theme.ts
Lines 75 to 77 in a38fa9a
wordpress-playground/packages/playground/blueprints/src/lib/steps/install-plugin.ts
Lines 72 to 74 in a38fa9a
fetch
wordpress-playground/packages/playground/blueprints/src/lib/resources.ts
Line 215 in a38fa9a
Possible solution: Prepare a proxy module/package that exports native DOM API for the browser and jsdom for Node.js.
The closest NPM package I found was
k0michi/isomorphic-dom
, which uses theexports
property in itspackage.json
to define browser and server exports.There's also
fetch-shim
which does the same for fetch andundici
; however, this latter module is now in Node.js core as globalfetch
since v18 so it's no longer necessary. EDIT: Just learned that globalFormData
is also available since Node.js v18. So it's onlyDOMParser
that needs to be polyfilled on the server side.The text was updated successfully, but these errors were encountered: