Skip to content

Commit

Permalink
Different attempt to fix #285
Browse files Browse the repository at this point in the history
  • Loading branch information
clayallsopp committed Aug 29, 2024
1 parent 2017a6a commit 97bd37a
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 3 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"test": "NODE_ENV=test BABEL_ENV=testing jest",
"storybook": "start-storybook -p 9001",
"deploy-storybook": "storybook-to-ghpages",
"build-storybook": "build-storybook -s public"
"build-storybook": "build-storybook -s public",
"postinstall": "patch-package"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -90,6 +91,8 @@
"husky": "^4.2.3",
"jest": "^27.5.1",
"mocha": "2.3.x",
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "^1.19.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
21 changes: 21 additions & 0 deletions patches/react-script-hook+1.6.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
diff --git a/node_modules/react-script-hook/lib/use-script.js b/node_modules/react-script-hook/lib/use-script.js
index b7ba6fc..e66de4b 100644
--- a/node_modules/react-script-hook/lib/use-script.js
+++ b/node_modules/react-script-hook/lib/use-script.js
@@ -93,6 +93,16 @@ function useScript(_a) {
return function () {
scriptEl.removeEventListener('load', handleLoad);
scriptEl.removeEventListener('error', handleError);
+
+ // if we unmount, and we are still loading the script, then
+ // remove from the DOM & cache so we have a clean slate next time.
+ // this is similar to the `removeOnUnmount` behavior of the TS useScript hook
+ // https://github.com/juliencrn/usehooks-ts/blob/20667273744a22dd2cd2c48c38cd3c10f254ae47/packages/usehooks-ts/src/useScript/useScript.ts#L134
+ // but only applied when loading
+ if (status && status.loading) {
+ scriptEl.remove();
+ delete exports.scripts[src];
+ }
};
// we need to ignore the attributes as they're a new object per call, so we'd never skip an effect call
}, [src]);
Loading

0 comments on commit 97bd37a

Please sign in to comment.