I’m not sure if I’ve set up my Browsersync correctly, or if I’m missing a configuration option, but I’m getting the following error when loading a local page via a jQuery AJAX call:
I’ve got a VHost set up for my local-only site, palmtree.studios.local but my Broswersync is running on localhost:3000 (default config). Below is my config.json file:
I know its a BrowserSync issue, because if I actually manually go to the palmtree.studios.local site (instead of localhost:3000) the AJAX call works normally and as intended. It’s as if, because the AJAX load page is called from palmtree.studios.local and the browsersync is loading it on localhost, even though they are technically the same thing, it is being read as a load from a 3rd party website.
Or something like that.
Can I set up my VHost/Browsersync in such a way that this won’t happen?
That’s correct, since localhost and palmtree.studios.local are different domains, as far as the browser is concerned it’s one website trying to load something from another one.
Thanks for the comments. Unfortunately, as listed in my config listed above, I already have both the “cors” and “headers” options enabled. I hope it’s not a syntax thing, I believe I am adding these correctly.
On a possible different end, is there any way to set up browsersync devUrl AND proxyUrl to the same domain?
Sorry about that, totally missed that you had it in there. I think you’ll need to put it in the Browsersync option in your webpack.config.watch.js file, though. In this section:
Not sure about using your local dev domain for browsersync. I feel like it would cause problems, but not sure. It would have to at least run on a separate port and that might still violate the cross-origin policy–I haven’t gone into the weeds there before.
Now that I’ve had a couple of minutes to actually think about this, I believe we’re approaching the problem from the wrong side. You need to enable CORS (set the Access-Control-Allow-Origin header) on whatever is hosting your local dev environment, not on Browsersync.
Your site is running on palmtree.studios.local, served by whatever local dev setup you have running
You’re loading the site at localhost:3000, served by Browsersync
The AJAX request from localhost:3000 hits palmtree.studios.local and palmtree.studios.local says “nope, don’t know you”
The cors Browsersync option tells Browsersync to be more relaxed when requests are made to Browsercync itself. You need the opposite: to tell whatever local hosting setup you’re running to relax, so that when Browsersync asks it for stuff it doesn’t get nervous and say no. For obvious reasons, request originators (Browsersync, in this case) can’t tell servers how secure to be.
So, you’ll need to figure out how to enable that header for whatever local dev setup you’re running.
I recently updated Apache for my WAMP instance. Turns out I didn’t have the headers module on and my vhost configuration wasn’t copied over because of that. Followed this:
And then created a .htaccess in the bedrock/web directory with:
I believe that still only adds the headers to the resources served by Browsersync from localhost:3000, not the resources requested from the local server, in which case it wouldn’t solve the problem discussed in this thread.
Hi,
I am having a similar issue, and I have tried all in this thread. Still can’t make it work…
I am using Sage 9 latest release.
I am using MAMP with apache vhosts.
<VirtualHost *:80>
DocumentRoot "/Users/Santos/sites/roots/web"
ServerName roots.localhost.com
<Directory "/Users/Santos/sites/roots/web">
AllowOverride All
# Allow access from bowsersync
Header set Access-Control-Allow-Origin "*"
</Directory>
</VirtualHost>
localhost:3000 keeps loading when I run yarn start and after several minutes loads the page, but styles are not loaded. I am not sure what the issue is and I have tried everything.
The chrome console shows the following, which I assume is ok and irrelevant, as styles return 404 he using live reload as mentioned in the docs:
After changing my devUrl to "devUrl": "http://localhost.com/roots/web",
hot reloading works. localhost.com points to /Users/Santos/sites.
So definitely the issue is related to the apache vhost.
But I cannot find out what’s wrong