-
maplibre-gl-js version: 4.7.1 browser: Mozilla/5.0 (Linux; Android 13; SM-G780G Build/TP1A.220624.014; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/131.0.6778.135 Mobile Safari/537.36 Context: When using CapacitorJS, you can enabled so called Example request that goes through CapacitorHttp; So it's something like a proxy within the app. Example request using CapacitorHttp (source); import { CapacitorHttp } from '@capacitor/core';
// Example of a GET request
const doGet = () => {
const options = {
url: 'https://example.com/my/api',
headers: { 'X-Fake-Header': 'Fake-Value' },
params: { size: 'XL' },
};
const response: HttpResponse = await CapacitorHttp.get(options);
// or...
// const response = await CapacitorHttp.request({ ...options, method: 'GET' })
};
// Example of a POST request. Note: data
// can be passed as a raw JS Object (must be JSON serializable)
const doPost = () => {
const options = {
url: 'https://example.com/my/api',
headers: { 'X-Fake-Header': 'Fake-Value' },
data: { foo: 'bar' },
};
const response: HttpResponse = await CapacitorHttp.post(options);
// or...
// const response = await CapacitorHttp.request({ ...options, method: 'POST' })
}; My main question is, can I in some way send requests through gl_ = Leaflet.maplibreGL({
style: `${host}/v4/maps/`,
pane: "maplibre-gl",
transformRequest: (url, resourceType) => {
if (!url.startsWith(host)) return { url };
return ["Tile", "Style"].includes(resourceType) ? {
url,
credentials: "include",
headers: {
"Authorization": `JWT ${token}`,
},
} : { url };
},
}); Also, why is CapacitorHttp's patch is not working on maplibre-gl? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
If capacitor doesn't psych correctly the fetch and xhr of the browser, then it's a question for capacitor, it might be related to cuddle running in worker thread if I needed to guess. |
Beta Was this translation helpful? Give feedback.
-
Okay I just tried some hack while writing this post, solution is at the bottom.I have been tinkering with this for a while now, looks like I am stuck in a order of compatibility problems; Packages;
Given this worker file (not working, just demonstration); // import { CapacitorHttp } from "@capacitor/core";
// importScripts("/@capacitor/core");
var CapacitorHttp;
import("@capacitor/core").then(module => {
CapacitorHttp = module.CapacitorHttp;
});
async function loadFn(params, abortController) {
const urlNext = params.url.split("://")[1];
const response = await CapacitorHttp.get({
url: `${urlNext.startsWith("localhost") ? "http" : "https"}://${urlNext}`,
headers: params.headers,
params: params.params,
})
console.log("Worker", params, response);
if (response.headers["content-type"] === "application/x-protobuf" && typeof response.data === "string") {
// base64ToBlob(response.data, "application/x-protobuf");
const bytes = new Uint8Array([...response.data].map(char => char.charCodeAt(0)));
// Now create a Blob from these bytes:
const blob = new Blob([bytes], { type: "application/x-protobuf" });
return {
data: await blob.arrayBuffer(),
}
}
return response;
}
self.addProtocol("capacitor", loadFn); Situation 1:Using Which I need to run worker with Imports look fine but it uses Situation 2:With some hacky usage I tried (according to previous results); importScriptInWorkers(new URL(workerUrl.replace("module", "classic"), import.meta.url).href) But it's not really helping; Uncaught (in promise) SyntaxError: Failed to execute 'importScripts' on 'WorkerGlobalScope': The URL '/@vite/env' is invalid. Situation 3:Vite docs mention that this specific syntax should import, but I can't use importScriptInWorkers(new URL("../workers/map-worker.js", import.meta.url)); This does not work, let's try using importScriptInWorkers(new URL("../workers/map-worker.js", import.meta.url).href) Okay it loads, but now, how do I import Looks like solved?Using Here is the final state of worker script; var CapacitorHttp;
import("@capacitor/core").then(module => {
CapacitorHttp = module.CapacitorHttp;
});
async function loadFn(params, abortController) {
const urlNext = params.url.split("://")[1];
const response = await CapacitorHttp.get({
url: `${urlNext.startsWith("localhost") ? "http" : "https"}://${urlNext}`,
headers: params.headers,
params: params.params,
})
console.log("Worker", params, response);
if (response.headers["content-type"] === "application/x-protobuf" && typeof response.data === "string") {
// base64ToBlob(response.data, "application/x-protobuf");
const bytes = new Uint8Array([...response.data].map(char => char.charCodeAt(0)));
// Now create a Blob from these bytes:
const blob = new Blob([bytes], { type: "application/x-protobuf" });
return {
data: await blob.arrayBuffer(),
}
}
return response;
}
self.addProtocol("capacitor", loadFn); And in my project; import { importScriptInWorkers } from "maplibre-gl";
...
gl_ = Leaflet.maplibreGL({
style: `${host}/v4/maps/`,
pane: "maplibre-gl",
transformRequest: (url, resourceType) => {
return ["Tile", "Style"].includes(resourceType) ? {
// This is important;
url: url.replace(/^https?/, "capacitor"),
credentials: "include",
headers: {
"Authorization": `JWT ${token}`,
},
} : { url };
},
});
importScriptInWorkers(new URL("../workers/map-worker.js", import.meta.url).href); |
Beta Was this translation helpful? Give feedback.
-
I think this is the first time I saw someone achieving to use addProtocol in the worker context. |
Beta Was this translation helpful? Give feedback.
Okay I just tried some hack while writing this post, solution is at the bottom.
I have been tinkering with this for a while now, looks like I am stuck in a order of compatibility problems;
Packages;
Given this worker file (not working, just demonstration);