Skip to content

Commit

Permalink
add: pathame to service-worker, to handle deploy with pathnames
Browse files Browse the repository at this point in the history
  • Loading branch information
h.u.g.u.rp committed Jul 16, 2023
1 parent 8d8b1c4 commit f7b637b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
9 changes: 6 additions & 3 deletions service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ self.symbols = {};
self.userSymbols = {};
self.allSymbols = {};
self.allEngines = [];
self.pathname = "/";

/* mock find/osd until we can import it */
const Find = {
Expand Down Expand Up @@ -85,9 +86,10 @@ const _mergeEngines = (allSymbols) => {

const _handleFetch = (event) => {
const url = new URL(event.request.url);
if (url.pathname === "/api") {
const localApiUrl = self.pathname + "api";
if (url.pathname === localApiUrl) {
return event.respondWith(_respondWithApiRoot(event.request));
} else if (url.pathname.startsWith("/api/suggestions")) {
} else if (url.pathname.startsWith(localApiUrl + "/suggestions")) {
return event.respondWith(_respondWithSuggestions(event.request));
} else {
return self.fetch(event.request.url);
Expand Down Expand Up @@ -124,13 +126,14 @@ const _respondWithApiRoot = (request) => {
};

const _handleMessage = ({ data }) => {
const { symbols, userSymbols } = JSON.parse(data);
const { symbols, userSymbols, location } = JSON.parse(data);
/* assign the data on the worker self global object;
so we can re-use these values in the suggestions */
self.symbols = symbols;
self.userSymbols = userSymbols;
self.allSymbols = _mergeSymbols(symbols, userSymbols);
self.allEngines = _mergeEngines(self.allSymbols);
self.pathname = pathname;
};

self.addEventListener("install", (event) => {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ if (isBrowser) {
JSON.stringify({
userSymbols: App.getUserSymbols(),
symbols: App.symbols,
pathname: window.location.pathname,
})
);
}
Expand Down

0 comments on commit f7b637b

Please sign in to comment.