|
2 | 2 |
|
3 | 3 | ## WebSDK Sandbox Environment |
4 | 4 |
|
| 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 | + |
5 | 10 | ### Run Instructions |
6 | 11 |
|
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: |
13 | 13 |
|
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 | + ``` |
16 | 20 |
|
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. |
20 | 22 |
|
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`). |
22 | 24 |
|
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.) |
26 | 26 |
|
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`. |
29 | 28 |
|
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`. |
31 | 30 |
|
32 | | -First: get a shell inside the container: `docker-compose exec onesignal-web-sdk-dev bash` |
| 31 | +2. Visit [https://localhost:4001?app_id=<your-app-id>](https://localhost:4001) and pass your OneSignal app id as a URL query parameter. |
33 | 32 |
|
34 | | -From OneSignal-Website-SDK (root) directory, build the SDK by running one of the following: |
| 33 | +### Build Instructions |
35 | 34 |
|
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: |
37 | 36 |
|
38 | 37 | ``` |
39 | | -npm run build:<type> |
| 38 | +vp run build:<env> |
40 | 39 | ``` |
41 | 40 |
|
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: |
48 | 42 |
|
49 | 43 | ``` |
50 | | -npm run build:<type>-<type> |
| 44 | +vp run build:<build-env>-<api-env> |
51 | 45 | ``` |
52 | 46 |
|
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`. |
54 | 48 |
|
55 | | -### CUSTOM ORIGIN PARAMS: |
| 49 | +### CUSTOM ORIGIN PARAMS |
56 | 50 |
|
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: |
58 | 52 |
|
59 | | -- BUILD_ORIGIN |
60 | | -- API_ORIGIN |
| 53 | +- `BUILD_ORIGIN` |
| 54 | +- `API_ORIGIN` |
61 | 55 |
|
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. |
63 | 57 |
|
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). |
65 | 59 |
|
66 | | -**Examples**: |
| 60 | +**Examples:** |
67 | 61 |
|
68 | 62 | ``` |
69 | | -API_ORIGIN=texas npm run build:dev-prod |
| 63 | +API_ORIGIN=texas vp run build:dev-prod |
70 | 64 | ``` |
71 | 65 |
|
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>`). |
73 | 67 |
|
74 | 68 | ``` |
75 | | -BUILD_ORIGIN=localhost API_ORIGIN=texas npm run build:dev-dev |
| 69 | +BUILD_ORIGIN=localhost API_ORIGIN=texas vp run build:dev-dev |
76 | 70 | ``` |
77 | 71 |
|
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>`. |
79 | 73 |
|
80 | 74 | ### HTTP |
81 | 75 |
|
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: |
83 | 77 |
|
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: |
85 | 83 |
|
86 | | -### NOTE ON PORTS: |
| 84 | +``` |
| 85 | +HTTPS=false vp run start |
| 86 | +``` |
87 | 87 |
|
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: |
89 | 93 |
|
90 | 94 | - HTTP: `4000` |
91 | 95 | - HTTPS: `4001` |
92 | 96 |
|
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/). |
94 | 100 |
|
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`). |
96 | 102 |
|
97 | | -### Running in Combination with OneSignal Container (dev-dev) |
| 103 | +### Running in combination with the OneSignal backend (dev-dev) |
98 | 104 |
|
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`: |
100 | 106 |
|
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. |
102 | 108 |
|
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: |
104 | 110 |
|
105 | 111 | ``` |
106 | 112 | // file: /etc/hosts |
107 | 113 |
|
108 | 114 | 192.168.40.21 texas |
109 | 115 | ``` |
110 | 116 |
|
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. |
112 | 118 |
|
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:<>-<>`. |
115 | 120 |
|
116 | 121 | ## Troubleshooting |
117 | 122 |
|
118 | 123 | ### Custom origin mismatch |
119 | 124 |
|
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). |
121 | 126 |
|
122 | 127 | ## Debugging Tips for OneSignal WebSDK Sandbox |
123 | 128 |
|
124 | | -### To clear your state: |
| 129 | +### To clear your state |
125 | 130 |
|
126 | | -1. Inspect -> Application -> Clear Site Data button |
| 131 | +1. Inspect → Application → Clear Site Data button |
127 | 132 |
|
128 | | - |
| 133 | +  |
129 | 134 |
|
130 | | -2. Click button left of URL, remove notification permission |
| 135 | +2. Click the button left of the URL, remove notification permission |
131 | 136 |
|
132 | | - |
| 137 | +  |
133 | 138 |
|
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.) |
135 | 140 |
|
136 | | -1. Inspect -> Network -> Disable Cache |
| 141 | +1. Inspect → Network → Disable Cache |
137 | 142 |
|
138 | | - |
| 143 | +  |
139 | 144 |
|
140 | 145 | 2. Refresh |
0 commit comments