Static site, CORS and javascript modules

I am working on a project that is a static site. The goal is to host javascript es6 module files. the project is using the es6 javascript module feature. Using a NODE server with allowing CORS, I can link to the modules (Script type=module) from my other site and the modules load.

However when loading the module directly from a glitch static site, the remote site rejects loading the file due to CORS issues.

If I change the file to a standard JS javascript file, it works.

I am curious if your static sites are configured to allow the javascript module type and allow * CORS?

It seems to be linked to this issue: Uploaded assets blocked by CORS, no `Access-Control-Allow-Origin` header

just need another file type to be CORS enabled.

Thanks

1 Like

Just to add, I changed the extension to .js and the module loaded. but then it stopped working.

Thanks

1 Like

I have to add something interesting. At first a script file marked as a module works. but in time it stops working. Some how like something changes about the file. Not sure if this gets into CDN or not.

Its a hard problem to describe. But i would summarize this a javscript module file is not loading properly as it gives the message in the console:

Access to script at ‘https://roam-quickref.glitch.me/rqr-main.mjs’ from origin ‘https://-- remote site removed–/’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

I think the only way to change the CORS headers would be to ask a Glitch staff member to adjust a few things in the s3 control panel. You could always contact support@glitch.com about this or you could make a feature request.

Thanks for your response.

1 Like

This kind of thing is made hard to debug, because the transition from project container active to project container inactive is built to be as seamless as possible. But it makes a difference in the response headers like this.

When you have the Glitch editor open, Glitch serves your project using a web server in the project container. A few minutes after you leave the editor, Glitch shuts down the project container and serves your project some other way–something with S3, if we are to believe the response headers.