Skip to content

Commit fa233ba

Browse files
chore(preview): modernize sandbox stack with Vite, mkcert, and env-aware start scripts (#1459)
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 0e09bb7 commit fa233ba

15 files changed

Lines changed: 416 additions & 5328 deletions

.devcontainer/devcontainer.json

Lines changed: 0 additions & 39 deletions
This file was deleted.

Dockerfile

Lines changed: 0 additions & 4 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Please reference the OneSignal SDK on your webpage via our CDN URL (listed in ou
1919

2020
## Local Development
2121

22-
Install the dependencies `npm install` then you can `npm run dev`. This will start a dev server on port 4001.
22+
Install the dependencies `vp install` then you can `vp run dev`. This will start a dev server on port 4001.
2323
The code will use navigator register on OneSignalSDKWorker.js to register the code as service worker.
2424

2525
## Preview
2626

27-
To preview with the actual bundle you can run the build first e.g. `npm run build:dev` then cd into the `preview` folder and follow the instructions in that folder. But generally you can run `docker-compose up` or `npm start`.
27+
To preview with the actual bundle, `cd preview && vp run start` — the script builds the SDK at the repo root and then starts the Vite-based sandbox server on `https://localhost:4001`. The default (`start:dev`) targets the production OneSignal API; `start:dev-stag` is also available for staging. See `preview/README.md` for the full setup.

docker-compose.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

docker/docker-entry-point.sh

Lines changed: 0 additions & 23 deletions
This file was deleted.

preview/.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

preview/README.md

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,139 +2,144 @@
22

33
## WebSDK Sandbox Environment
44

5+
A Vite-based sandbox for exercising a built OneSignal Web SDK against a real browser. The dev server is configured in `preview/vite.config.ts` and serves:
6+
7+
- `index.html`, `manifest.json`, `sw.js`, and the `/push/onesignal/` worker out of this folder
8+
- `/sdks/web/v16/<file>` mapped to the SDK build output at `../build/releases/<file>`, with the filename prefix (`Dev-` / `Staging-` / unprefixed) chosen at runtime via `SDK_ENV`
9+
510
### Run Instructions
611

7-
1. `docker-compose up`
8-
- If SSL certs need to be created, this will be done for you automatically with the common name default setting `localhost` and alternative DNS names (this can be customized in `certs/gen-cert.sh`):
9-
- texas
10-
- california
11-
- oregon
12-
- washington
12+
1. From `preview/`, pick the script that matches what you want to test. Each script builds the SDK at the repo root first, then starts the dev server:
1313

14-
2. Follow the following instructions (also logged in console)
15-
- copy `dev-ssl.crt` from container to host with:
14+
```
15+
cd preview
16+
vp run start # alias for start:dev
17+
vp run start:dev # local SDK against the production OneSignal API
18+
vp run start:dev-stag # local SDK against staging (API_TYPE=staging, API_ORIGIN=onesignal.com)
19+
```
1620

17-
```
18-
docker cp "$(docker-compose ps -q onesignal-web-sdk-dev)":sdk/preview/certs/dev-ssl.crt .
19-
```
21+
Both scripts produce a `BUILD_TYPE=development` build, so the SDK shim loads its ES6 bundle from `localhost:4001` rather than `cdn.onesignal.com`. They only differ in which OneSignal API origin the SDK talks to.
2022

21-
- If you're running the container in a VM, get the cert file onto the VM's host (e.g: use `scp`)
23+
`start:dev` calls the production API even though it's labeled "dev". This is because `build:dev` doesn't set `API`, and `__API_TYPE__` falls back to `'production'` at runtime via `src/shared/utils/env.ts`. If you need to point the SDK at a locally-running OneSignal backend, you'll need to also set `API=development API_ORIGIN=<host>` at build time (see `build:dev-dev` in the root `package.json`).
2224

23-
- Add cert to system's trusted store
24-
- macOS: `sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain dev-ssl.crt`
25-
- Windows: `Import-Certificate -FilePath dev-ssl.crt -CertStoreLocation cert:\CurrentUser\Root`
25+
The sandbox's `index.html` and `OneSignalSDKWorker.js` always reference `Dev-OneSignalSDK.*` URLs; the dev server's middleware rewrites the prefix on the way through based on `SDK_ENV` so the same HTML works against any build flavor. (A true `BUILD_TYPE=production` build is not wired up here because the prod shim hardcodes `cdn.onesignal.com` as its source — running it locally would just smoke-test the shim while loading the deployed prod SDK from CDN. Use `vp run build:prod` from the repo root if you specifically need to verify the prod shim + `size-limit` gates.)
2626

27-
3. Make sure the common name (e.g: localhost, texas, oregon, etc...) maps to the correct IP in your `/etc/hosts` file
28-
4. Visit [https://localhost:4001?app_id=](https://localhost:4001?app_id=) and insert your app id as a URL query parameter
27+
If you'd rather skip the rebuild (already-built bytes in `build/releases/`), invoke `SDK_ENV=<dev|staging|production> vp dev` directly from `preview/`. For live rebuilds during SDK iteration, run `vp run build:dev:watch` from the repo root in another terminal alongside `vp dev`.
2928

30-
### Build Instructions
29+
On first run, [vite-plugin-mkcert](https://github.com/liuweiGL/vite-plugin-mkcert) downloads the `mkcert` binary, installs a local root CA into your system trust store, and issues a cert for `localhost` (plus a few common hostname aliases). After that, every preview run reuses the same trusted cert with no manual steps. If you'd rather pre-install `mkcert` yourself: `brew install mkcert` on macOS, then `mkcert -install`.
3130

32-
First: get a shell inside the container: `docker-compose exec onesignal-web-sdk-dev bash`
31+
2. Visit [https://localhost:4001?app_id=&lt;your-app-id&gt;](https://localhost:4001) and pass your OneSignal app id as a URL query parameter.
3332

34-
From OneSignal-Website-SDK (root) directory, build the SDK by running one of the following:
33+
### Build Instructions
3534

36-
1. This will assign the environment type to both the BUILD and API environment types
35+
From the repo root, build the SDK with one of:
3736

3837
```
39-
npm run build:<type>
38+
vp run build:<env>
4039
```
4140

42-
- Options are:
43-
- `dev`
44-
- `staging`
45-
- `prod`
46-
47-
2. This will assign the environment type accordingly:
41+
Where `<env>` is `dev`, `staging`, or `prod`. Or set BUILD and API environments independently:
4842

4943
```
50-
npm run build:<type>-<type>
44+
vp run build:<build-env>-<api-env>
5145
```
5246

53-
**Example**: `npm run build:dev-prod` builds the SDK with the BUILD environment as "development" and the API environment as "production"
47+
**Example:** `vp run build:dev-prod` builds with the BUILD environment as `development` and the API environment as `production`.
5448

55-
### CUSTOM ORIGIN PARAMS:
49+
### CUSTOM ORIGIN PARAMS
5650

57-
You can pass two additional variables to the above command, the first being the origin of the build environment and the second being that of the api environment. These env vars are:
51+
You can override the build and API origins via environment variables:
5852

59-
- BUILD_ORIGIN
60-
- API_ORIGIN
53+
- `BUILD_ORIGIN`
54+
- `API_ORIGIN`
6155

62-
If no custom origins are set, defaults will be used: `localhost` for build and `onesignal.com` for api.
56+
Defaults are `localhost` for build and `onesignal.com` for api.
6357

64-
**Note:** make sure custom origins make sense with respect to the build and api environments set (e.g: you cannot use a `prod` api environment and expect a custom api origin to be used).
58+
**Note:** make sure custom origins make sense with respect to the build and api environments set (e.g. you cannot use a `prod` api environment and expect a custom api origin to be used).
6559

66-
**Examples**:
60+
**Examples:**
6761

6862
```
69-
API_ORIGIN=texas npm run build:dev-prod
63+
API_ORIGIN=texas vp run build:dev-prod
7064
```
7165

72-
This sets the BUILD environment origin to `texas` which will result in SDK files being fetched from `https://texas:4001/sdks/web/v##/` and the API environment origin to production which will make all onesignal api calls to the production origin `https://api.onesignal.com/apps/<app>`
66+
Sets the BUILD environment origin to `texas` (so SDK files are fetched from `https://texas:4001/sdks/web/v##/`) and the API environment origin to production (so all OneSignal API calls go to `https://api.onesignal.com/apps/<app>`).
7367

7468
```
75-
BUILD_ORIGIN=localhost API_ORIGIN=texas npm run build:dev-dev
69+
BUILD_ORIGIN=localhost API_ORIGIN=texas vp run build:dev-dev
7670
```
7771

78-
This sets the BUILD environment origin to `localhost` which will result in SDK files being fetched from `https://localhost:4001/sdks/web/v##/` and the API environment origin to the default `https://texas:3001/api/v1/apps/<app_id>`
72+
Sets the BUILD origin to `localhost` (SDK files from `https://localhost:4001/sdks/web/v##/`) and the API origin to `https://texas:3001/api/v1/apps/<app_id>`.
7973

8074
### HTTP
8175

82-
All builds default to `https` unless `HTTPS=false` is passed to the build command...
76+
All builds default to `https` unless `HTTPS=false` is passed:
8377

84-
**Example**: `HTTPS=false npm run build:dev-prod` or `HTTPS=false BUILD_ORIGIN=localhost npm run build:dev-prod`
78+
```
79+
HTTPS=false vp run build:dev-prod
80+
```
81+
82+
The preview server itself also accepts `HTTPS=false` to bind HTTP on port 4000 instead of HTTPS on 4001:
8583

86-
### NOTE ON PORTS:
84+
```
85+
HTTPS=false vp run start
86+
```
8787

88-
**SDK**: SDK files will automatically be fetched from the 4000s ports depending on the HTTP/S setting
88+
Note: the service worker import path in `push/onesignal/OneSignalSDKWorker.js` is hard-coded to `https://localhost:4001/...`, so push subscription flows require HTTPS mode.
89+
90+
### NOTE ON PORTS
91+
92+
**SDK:** SDK files are fetched from the 4000s ports depending on the HTTP/S setting:
8993

9094
- HTTP: `4000`
9195
- HTTPS: `4001`
9296

93-
Use the **`NO_DEV_PORT=true`** build env var to build without a port number. This is useful when using a reverse proxy like [ngrok](https://ngrok.com/) to serve your localhost environment on the web.
97+
These ports are hardcoded in `src/page/utils/shimLoader.ts`, so the preview server in `preview/vite.config.ts` is pinned to match. Changing one without the other will silently break the dev flow.
98+
99+
Use `NO_DEV_PORT=true` at build time to omit the port number entirely. Useful with reverse proxies like [ngrok](https://ngrok.com/).
94100

95-
**API**: dev-environment API calls will be made to the `3001` port (e.g: `<custom-origin>:3001`)
101+
**API:** dev-environment API calls go to port `3001` (e.g. `<custom-origin>:3001`).
96102

97-
### Running in Combination with OneSignal Container (dev-dev)
103+
### Running in combination with the OneSignal backend (dev-dev)
98104

99-
You may want to run the Web SDK Sandbox with the configuration `dev-dev`. You will need to make sure to follow some steps first so that both the **Express Webpack** and **OneSignal** containers work in conjunction...
105+
To run the WebSDK sandbox against a local OneSignal backend with `dev-dev`:
100106

101-
1. Make sure your browser can make secure connections to wherever your OneSignal container is running, VM or otherwise. This may involve adding a cert to your keychain and/or visiting the container's frontend (Dashboard) and allowing the browser to proceed past the "unsafe" warning.
107+
1. Make sure your browser can make secure connections to wherever your OneSignal backend is running. This may involve adding a cert to your keychain and/or visiting the backend's frontend (Dashboard) and allowing the browser to proceed past the "unsafe" warning.
102108

103-
2. If you are running the OneSignal container on a machine with a different IP address, add an alias to your hosts file which you can then use easily in your API environment option at build time.
109+
2. If the OneSignal backend is on a different machine, add a hosts alias you can pass as the API environment option at build time:
104110

105111
```
106112
// file: /etc/hosts
107113
108114
192.168.40.21 texas
109115
```
110116

111-
See above for example on using `texas` as the API environment option
117+
See above for an example using `texas` as the API environment option.
112118

113-
3. Make sure that the OneSignal container is using the URLs to your WebSDK container's build files. To do this,
114-
- change the URLs in the file `development.rb` so that they point to the files' absolute paths (these are the files in the `build` directory after running `npm run build:<>-<>`)
119+
3. Make sure the OneSignal backend is using URLs to your locally-served WebSDK build files. Update the URLs in `development.rb` to point to the absolute paths in the `build` directory after running `vp run build:<>-<>`.
115120

116121
## Troubleshooting
117122

118123
### Custom origin mismatch
119124

120-
Check the network tab in the browser dev tools to see what origin the SDK is using for network calls. If you set the `API_ORIGIN` var to something other than `onesignal.com` but it is still using that, make sure you are using the correct build command. For example, if you set the origin to `staging.onesignal.com` you should _not_ be using the `dev-prod` environment since the `prod` will result in the ignoring of the custom origin parameter. The fix in this case would be to use `dev-stag`.
125+
Check the network tab in browser dev tools to see which origin the SDK is using for network calls. If you set `API_ORIGIN` to something other than `onesignal.com` but the SDK is still using that, double-check the build command. For example, setting the origin to `staging.onesignal.com` requires `dev-stag`, not `dev-prod` (the `prod` environment ignores the custom api origin parameter).
121126

122127
## Debugging Tips for OneSignal WebSDK Sandbox
123128

124-
### To clear your state:
129+
### To clear your state
125130

126-
1. Inspect -> Application -> Clear Site Data button
131+
1. Inspect Application Clear Site Data button
127132

128-
![image](https://github.com/user-attachments/assets/701e3da1-0c15-4940-a47b-feab55e2b953)
133+
![image](https://github.com/user-attachments/assets/701e3da1-0c15-4940-a47b-feab55e2b953)
129134

130-
2. Click button left of URL, remove notification permission
135+
2. Click the button left of the URL, remove notification permission
131136

132-
![image](https://github.com/user-attachments/assets/72d718a6-15da-4261-8919-2a2b565d1db3)
137+
![image](https://github.com/user-attachments/assets/72d718a6-15da-4261-8919-2a2b565d1db3)
133138

134-
### In the event of weird errors (can not find your application, it is not configured correctly, etc):
139+
### In the event of weird errors (cannot find your application, it is not configured correctly, etc.)
135140

136-
1. Inspect -> Network -> Disable Cache
141+
1. Inspect Network Disable Cache
137142

138-
![image](https://github.com/user-attachments/assets/83950e91-7278-4f96-8c75-577797cc9697)
143+
![image](https://github.com/user-attachments/assets/83950e91-7278-4f96-8c75-577797cc9697)
139144

140145
2. Refresh

preview/certs/gen-cert.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

preview/certs/ssl-cert-gen.conf

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)