Skip to content

Commit

Permalink
- skipWaiting() + check cookieStore
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Sep 27, 2024
1 parent b74ada9 commit 2499a3c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## master

## 0.1.3

- Check if `cookieStore` is available and only manipulate cookies if it is.

- Add `skipWaiting()` to the server worker to force the new version to take over.

## 0.1.2

- Add cache support to Rack handler.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Here is an example app:

This gem provides a variety of _adapters_ and plugins to make your Rails application Wasm-compatible:

- `Kernel#on_wasm?`: a convenient predicate method to check if the code is running in the Wasm environment.

- Active Record

- `sqlite3_wasm` adapter: work with `sqlite3` Wasm just like with a regular SQLite database.
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/wasmify/pwa/templates/pwa/rails.sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ self.addEventListener("activate", (event) => {

self.addEventListener("install", (event) => {
console.log("[rails-web] Install Service Worker");
event.waitUntil(installApp());
event.waitUntil(installApp().then(() => self.skipWaiting()));
});

const rackHandler = new RackHandler(initVM, { assumeSSL: true });
Expand Down
2 changes: 1 addition & 1 deletion lib/wasmify/rails/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Wasmify
module Rails # :nodoc:
VERSION = "0.1.2"
VERSION = "0.1.3"
end
end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wasmify-rails",
"version": "0.1.2",
"version": "0.1.3",
"description": "JS utilities for wasmify-rails",
"main": "index.js",
"engines": {
Expand Down
42 changes: 25 additions & 17 deletions src/rack.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export class RackHandler {
this.quiteAssets = opts.quiteAssets || true;
this.assumeSSL = opts.assumeSSL || false;
this.vmSetup = vmSetup;
// Check if cookieStore is supported
this.cookiesEnabled = !!globalThis.cookieStore;
this.queue = new RequestQueue(this.process.bind(this));
}

Expand Down Expand Up @@ -138,10 +140,14 @@ export class RackHandler {
}

try {
const cookies = await cookieStore.getAll();
const railsCookie = cookies.map((c) => `${c.name}=${c.value}`).join("; ");
if (this.cookiesEnabled) {
const cookies = await cookieStore.getAll();
const railsCookie = cookies
.map((c) => `${c.name}=${c.value}`)
.join("; ");

railsHeaders["HTTP_COOKIE"] = railsCookie;
railsHeaders["HTTP_COOKIE"] = railsCookie;
}

let input = null;

Expand Down Expand Up @@ -227,22 +233,24 @@ export class RackHandler {

let { status, headers, body } = res;

const cookie = headers["set-cookie"];
if (this.cookiesEnabled) {
const cookie = headers["set-cookie"];

if (cookie) {
const cookies = setCookieParser.parse(cookie, {
decodeValues: false,
});
cookies.forEach(async (c) => {
await cookieStore.set({
name: c.name,
value: c.value,
domain: c.domain,
path: c.path,
expires: c.expires,
sameSite: c.sameSite.toLowerCase(),
if (cookie) {
const cookies = setCookieParser.parse(cookie, {
decodeValues: false,
});
});
cookies.forEach(async (c) => {
await cookieStore.set({
name: c.name,
value: c.value,
domain: c.domain,
path: c.path,
expires: c.expires,
sameSite: c.sameSite.toLowerCase(),
});
});
}
}

// Convert image into a blob
Expand Down

0 comments on commit 2499a3c

Please sign in to comment.