-
-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Using the script directly breaks assets #3708
Comments
I don’t see why this question would be related to CRA. Open the Network tab and check which request is getting a 404. Then you need to figure out why the server isn’t serving what you expect at the path you expect. Hope this helps! |
@gaearon The error I get is because it's searching for But |
It uses an absolute path but absolute paths are calculated from the host that served the page. If you want to hardcode a specific URL to calculate them against, you need to set |
@gaearon
And that's what I'm trying to avoid... 😕 |
Do you require routing? If not, you can try to set the homepage to |
@Timer Yeah we use I'm open to any given solution that could help us. 🙂 The way we deploy right now is:
|
Your best solution is as described above, use |
@Timer |
@Timer so for both staging and production, you'll have the same But the problem is that the script is loaded @ |
This is going to be your best solution: #3708 (comment) |
Too bad 😕 There's also not an option to set the |
No, it needs to be embedded in the bundle. |
Too bad 😕 It's just stupid to build it 2 times, when the code is exactly the same, except for the |
What’s your proposal to fix this? |
@gaearon Can't think of a good way on how to fix this one right now 😕 If it would be a normal app (just visit it in the browser or via an |
To be honest the whole thing with copying the script sounds like you're stretching the tool to do something it doesn't support. Maybe it's better to use something more flexible (e.g. nwb or Neutrino)? |
@gaearon The copying of the script is to support both ways
I don't think it's that weird if you think about it like that isn't it? |
Maybe. I'm struggling to understand what happens, sorry. Could you explain it from the beginning: what you need to do, why you have two servers, how you build it, what ends up 404'ing. Thanks. |
@gaearon OK I'll try to explain it as clear as possible 🙂 I'm building a react app (using CRA) that's going to be used as a plugin. This need to be possible in 2 different ways:
<iframe src="https://plugin.domain.com/plugin-name/version-number?param1=value1¶m2=value2"></iframe>
<div id="plugin-name" data-param1="value1" data-param2="value2"></div>
<script type="text/javascript" src="https://plugin.domain.com/plugin-name/version-number/main.js"></script> So in order to get this behavior, I use the default When releasing a new version, I use the following workflow to deploy the app:
When then using the Building the app 2 times with a different I hope you understand my use case a bit more. |
@MichaelDeBoey maybe setting |
@viankakrisna That could maybe be a solution indeed. 🙂 I'm also looking into setting // src/index.js
import './public-path';
// ... // src/public-path.js
__webpack_public_path__ = resolvePublicPath();
function resolvePublicPath() {
// check the file path of the current executing script and derive the root-folder
// Ref: https://stackoverflow.com/questions/2255689/how-to-get-the-file-path-of-the-currently-executing-javascript-code
} And dropping the |
Tbh this sounds a bit too esoteric to me. I think it’s cool if this workaround is working for you but I don’t think we’ll be embracing something like this as a first-class feature. Usually when you need something like this you end up needing even more customizations, and at that point ejecting, forking or “rewiring” but be better anyway.
I think I understand the problem you're describing with |
@gaearon Well I sort of have 2 problems indeed, but they're related Problem 1 is the breaking assets when using the But solving it right that causes a new problem, which is that I have to build the app 2 times, because the |
The only other solution I can see is #3708 (comment). It won't work for you if you use Other than that I don’t really see other good options. |
@gaearon I'm using client-side routing ( I've already looked into setting I really think this could be the default implementation for the Implementation would be something like // src/index.js
import './public-path';
// ... // src/public-path.js
__webpack_public_path__ = resolvePublicPath();
function resolvePublicPath() {
const currentScript = document.currentScript || getCurrentScriptViaFallback();
const publicPath = currentScript.split('/static/js')[0] // app root directory
.split('main.')[0]; // add support for script in app root directory
return `${publicPath}/`;
} |
Maybe. I'm afraid I'm out of suggestions because I don't know how |
@gaearon I found it in their docs 🙂 |
@gaearon I don't know how I think this will probably (as a default) be the best solution instead of deriving it from |
I'm not sure. If you can write a proposal in another issue with how it should work instead we can take a look. |
@gaearon OK I'll try to submit one asap 🙂 |
@MichaelDeBoey you might want to have a look at this: https://itnext.io/so-you-want-to-host-your-single-age-react-app-on-github-pages-a826ab01e48 If you want to pre-deploy or deploy you could even have a bash script to take care of the PUBLIC_URL parameters. FYI @gaearon |
Hi there!
So I'm having the following situation:
I created an app using
create-react-app
(this repo), which has some assets.The app is used to work as a plugin on many different sites/apps.
So the purpose is to provide it in 2 possible ways:
div
with the correctid
and a separate<script>
tag that points to the script (which is duplicated fromstatic/js/main.*.js
tomain.js
in the root) and pass params via thedataset
When using the
iframe
, everything works like expected. But when using thediv
+script
, I get the following error:Any idea how I can fix this or what the preferred way of doing something like this is?
CC: @gaearon @Timer
The text was updated successfully, but these errors were encountered: