cables DocumentationHow To UseWorking with filesKeyboard ShortcutsUser Interface WalkthroughBeginner TutorialBeginner 1: Drawing A CircleBeginner 2: TransformationsBeginner 3: ColorMore TransformationsIntermediateImage CompositionsPost-Processing 3D ScenesExporting And EmbeddingHow to serve files externally and fix CORS headersExporting PatchesExport using the cables command line interfaceExport via IframeExport creating a standalone executableExport to github (pages)Export and deploy to netlifyExport full PatchExport a ZIP fileExternal triggers / functionsUsing variablesPreviewing / uploading exported cables patchesExamples for EmbeddingPermissionsUsersPatchesTeamsOpsMultiplayerPatchlistsCoding OpsCreating AttachmentsGeneral op/Port CallbacksPortsDynamic PortsArray PortsBoolean portsInteger Number PortsObject PortsString portsTrigger PortsFloating Point Number PortsGUI/UI attributesHello Op - Part 1LibrariesDeveloping OpsRenaming / Creating a new versionCreating Viz OpsGuidelinesObject PortsPatching Ops / SubPatchOpsWriting ShadersWeb Audio Op DevelopmentDeveloping CablesRepositoriesSet up local environmentScriptsHow to work with forksGenerated DocumentationUsing standalone to develop cablesCables StandaloneGeneralCoding OpsSharing opsUsing NPMImport/ExportStandalone - FAQLightingLightsShadowsWorking With AudioBasic Audio SetupWorking with EffectsReal-Time Audio Analyzation & Audio VisualizationOffline Audio Visualization & AnalyzationCommunicationcables APIOptimizing Performance In PatchesTools for debugging your patchHow to optimize cables patches with the Performance opHow to optimize cables patches with the ProfilerCommon pitfalls with the "usual suspects"Optimizing arraysDebugging Shaders with ShaderInfoFAQAudio in web browsersHow to make demoscene demos with cables.glEmbeddingHow to integrate my cables patch into my CMS (webflow/wix/squarespace/...)?How to remove grey rectangles on touch (mobile)?Why doesn't the DownloadTexture op work on iOS?How to disable page scrolling on mobile?Mobile tippsHow to run exported patches on your local machineTransparent CanvasFeatures and SupportHow to contribute code to cablesWill there be support for (animated) GIFs?Can i use a website as a texture?Screenshots and Video recordingHow to report a bug in cablesHow can I share a patch to get help?How can I support cables?Video playback in cablesGeneral questionsWhat is dev.cables.glHTML And CSS In CablesJavascript Frameworkscordova / phonegapelectronreactvuejsLicenses and paymentWhat license do i need to use cables?Will I have to pay for it in the future?How is my work licensed when using cables?Does cables support midi and OSC?Patch PermissionsMy User Profile & Social MediaShadertoyCables at schools and universitiesTechnical questionsWebGL1 and WebGL2

How to serve files externally and fix CORS headers

When does this happen?

Sometimes you want to access resources on different servers than your patch is running on, these may be images, some api or any different file. Check this patch for example, that loads data from an API to get the current temperature in Berlin.

For this the provider of the API has to allow for access of the data from a browser by providing certain HTTP-Headers (see below). Really often this is not the case.

How do i know this happens to me?

Use this cors test to see if your headers are set correctly: https://cors-test.codehappy.dev/

First of all, you won't see the files loaded, no images, no data from the API. Errors like "unexpected end of JSON". Check your developer tools (ctrl+shift+i or cmd+option+i) console in the browser and you will, most likely, see something like this:

Access to XMLHttpRequest at 'https://www.metaweather.com/api/location/638242/' from origin 'https://devsandbox.cables.gl'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This means the provider of the data/images (you?) has not set the correct headers (yet). This is only fixable on the serverside, not in cables.

What is CORS?

Currently, client-side scripts (e.g., JavaScript) are prevented from accessing much of the Web of Linked Data due to "same origin" restrictions implemented in all major Web browsers.

While enabling such access is important for all data, it is especially important for Linked Open Data and related services; without this, our data simply is not open to all clients.

CORS gives the providers of data on webservers a way to tell the browser to load/not load that resource.

How to fix this?

This page gives a comprehensive overview on how to fix this on the side of the provider of the files in various webservers and programming languages. It basically boils down to adding this to your HTTP-Response headers:

Access-Control-Allow-Origin: https://cables.gl
Access-Control-Allow-Origin: https://sandbox.cables.gl
Access-Control-Allow-Origin: https://devsandbox.cables.gl

or more broadly:

Access-Control-Allow-Origin: *

Okay, I did this, still does not work, what else?

So, let's assume you are using HttpRequest. Check that you do not have the JSONP-Checkbox toggled on. Once that is on, all the above does not cause any problems, but your browser is loading the external ressouce as if it were a JavaScript-File. We cannot control these requests and we also cannot send any additional headers with them.

Next thing to check is your network tab in the dev-tools of the browser you are using. Is there any request failing? If there is, check wether or not the "method" is given as "OPTIONS". Then fix these errors.

OPTIONS?!

Before any CORS request the browser is advised to do an additional request (a so-called "preflight"), using the HTTP "OPTIONS" method (instead of "GET" for example). If that fails, the browser will not attempt to "GET" the requested ressouce.

Make sure that the/your API you are talking to answers properly to preflight requests. If you are dealing with PHP, a very hacky way of doing this is something like:

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  header("HTTP/1.1 200 OK");
}

This greatly varies with webservers, languages, CMS versions, frameworks and everything around it though, so try to find a way to set this in a sensible way for your setup.


Found a problem? Edit this file on github and contribute to cables!