|
| 1 | +# Handling CORS Issues with Static Assets |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +When running a headless frontend on a different domain than your WordPress site, you may encounter [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) issues with static assets served directly from the WordPress server. |
| 6 | + |
| 7 | +By default, browsers block requests to resources from a different origin unless explicitly allowed. |
| 8 | + |
| 9 | +Built-in WordPress hooks and 3rd-party plugins such as [WPGraphQL CORS](https://github.com/funkhaus/wp-graphql-cors) or [WPGraphQL Headless Login](https://github.com/axewp/wp-graphql-headless-login) can help set proper `Access-Control-*` headers for requests generated _via_ WordPress, but loading assets such as images, fonts, and other static files accessed directly from the server may require manual configuration. |
| 10 | + |
| 11 | +## Local Development |
| 12 | + |
| 13 | +For local development, the easiest thing to do is to use a browser extension to disable CORS restrictions (e.g., a Chrome extension). |
| 14 | + |
| 15 | +However, while this is a quick fix for development, it is not a viable solution for production. Instead, you should configure your web server to include the necessary CORS headers. |
| 16 | + |
| 17 | +## Configuring CORS Headers on Server Assets |
| 18 | + |
| 19 | +To resolve CORS issues in a production environment, configure your web server to include the necessary CORS headers. Below are configurations for **Nginx** and **Apache**. |
| 20 | + |
| 21 | +### Apache |
| 22 | + |
| 23 | +If using Apache, add the following configuration to your `.htaccess` file or Apache configuration: |
| 24 | + |
| 25 | +```apacheconf |
| 26 | +<FilesMatch "\.(js|css|woff2?|ttf|eot|svg|gif|jpg|jpeg|png|ico|webp)$"> |
| 27 | + Header always set Access-Control-Allow-Origin "https://example.com" # Replace example.com with the frontend domain. |
| 28 | +</FilesMatch> |
| 29 | +``` |
| 30 | + |
| 31 | +After updating the configuration, reload Apache. E.g., |
| 32 | + |
| 33 | +```sh |
| 34 | +sudo systemctl reload apache2 |
| 35 | +``` |
| 36 | + |
| 37 | +### Nginx |
| 38 | + |
| 39 | +Add the following configuration to your Nginx server block to allow CORS for static assets: |
| 40 | + |
| 41 | +```nginx |
| 42 | +location ~* \.(js|css|woff2?|ttf|eot|svg|gif|jpg|jpeg|png|ico|webp)$ { |
| 43 | + set $cors_origin ""; |
| 44 | +
|
| 45 | + if ($http_origin ~* "^https?://(www\.)?example\.com$") { # Replace example.com with the frontend domain |
| 46 | + set $cors_origin $http_origin; |
| 47 | + } |
| 48 | +
|
| 49 | + add_header 'Access-Control-Allow-Origin' "$cors_origin"; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +After updating the configuration, reload Nginx. E.g., |
| 54 | + |
| 55 | +```sh |
| 56 | +sudo systemctl reload nginx |
| 57 | +``` |
| 58 | + |
| 59 | +## Using SnapWP CORS Middleware |
| 60 | + |
| 61 | +To further mitigate CORS issues, SnapWP includes a CORS middleware that proxies requests for script modules to the WordPress server. This bypasses CORS restrictions by routing requests through the Next.js server. |
| 62 | + |
| 63 | +### Enabling the CORS Middleware |
| 64 | + |
| 65 | +To enable the CORS proxy feature, update your `snapwp.config.mjs` file: |
| 66 | + |
| 67 | +```javascript |
| 68 | +/** @type {import('@snapwp/core/config').SnapWPConfig} */ |
| 69 | +const config = { |
| 70 | + // Other configuration options |
| 71 | + useCorsProxy: true, |
| 72 | +}; |
| 73 | + |
| 74 | +export default config; |
| 75 | +``` |
| 76 | + |
| 77 | +### Customizing the Proxy Prefix |
| 78 | + |
| 79 | +By default, the proxy prefix is set to `/proxy`. If needed, you can override this by specifying `corsProxyPrefix` in the configuration. |
| 80 | + |
| 81 | +```javascript |
| 82 | +const config = { |
| 83 | + useCorsProxy: process.env.NODE_ENV !== 'production', // Enable CORS proxy in nonproduction environments. |
| 84 | + corsProxyPrefix: '/custom-proxy', // Optional custom prefix |
| 85 | +}; |
| 86 | +``` |
0 commit comments