Skip to content

Commit 7c15ebd

Browse files
SH4LINjustlevine
andauthored
docs: add how-to for Handling CORS Issues with Static Assets (#40)
* docs: cors.md document added * chore: cors.md formatted * chore: refocus content and snippets * docs: update installation steps to reference cors.md --------- Co-authored-by: Dovid Levine <david@axepress.dev>
1 parent 9d67ad9 commit 7c15ebd

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

docs/cors.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
```

docs/getting-started.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ This guide will walk you through setting up a headless WordPress app using SnapW
2020
wp plugin install wp-graphql https://github.com/wpengine/wp-graphql-content-blocks/releases/latest/download/wp-graphql-content-blocks.zip https://github.com/rtCamp/snapwp-helper/releases/latest/download/snapwp-helper.zip --activate
2121
```
2222

23-
2. (Optional) If you are faced with CORS issues during local development you may want to install activate the [`Allow CORS: Access-Control-Allow-Origin` Chrome Extension](https://chromewebstore.google.com/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf) or similar.
23+
2. (Optional) If you're running your WordPress site on a different domain than your frontend, you may need to [configure CORS headers](./cors.md).
2424
2525
## Frontend Setup
2626
2727
### Prerequisites
2828
29-
- **Node.js**: v20+ (with `npm and `npx` installed).
29+
- **Node.js**: v20+ (with `npm` and `npx` installed).
3030
- **A WordPress backend** [configured with SnapWP Helper](#backend-setup).
3131
3232
### Installation Steps
@@ -75,19 +75,19 @@ To create a new headless WordPress app using SnapWP, follow these steps:
7575
7676
@todo
7777
78-
## **Additional Resources [Reference]**
78+
## Additional Resources
7979
8080
This section contains a list of curated resources for developers working with headless WordPress, WPGraphQL, and some of the technologies used in the SnapWP stack.
8181
82-
### **WPGraphQL**
82+
### WPGraphQL
8383
8484
- **Docs**: [https://www.wpgraphql.com/docs/introduction](https://www.wpgraphql.com/docs/introduction)
8585
- **Official Discord Community**: [https://wpgraphql.com/discord/](https://wpgraphql.com/community/)
8686
87-
### **Next.js**
87+
### Next.js
8888
8989
- **Docs**: [https://nextjs.org/docs](https://nextjs.org/docs)
9090
91-
### **TypeScript**
91+
### TypeScript
9292
9393
- **Docs**: [https://www.typescriptlang.org/docs/](https://www.typescriptlang.org/docs/)

0 commit comments

Comments
 (0)