Skip to content

Commit

Permalink
support old browser without CompressionStream
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed May 15, 2024
1 parent cb89f04 commit 5b6c1e5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const extractTextNodes = require('./utils/translation/extractTextNodes.js');
const getTranslationsFromAPI = require('./utils/translation/getTranslationsFromAPI.js');
const { renderWeploySelectorState } = require('./utils/selector/renderWeploySelectorState.js');
const getTranslationCacheFromCloudflare = require('./utils/translation/getTranslationCacheFromCloudflare.js');
const { isCompressionSupported } = require('./utils/compressions.js');

var isDomListenerAdded;

Expand Down Expand Up @@ -119,7 +120,7 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {
window.weployTranslating = true;
renderWeploySelectorState({ shouldUpdateActiveLang: false });

const cacheFromCloudFlare = await getTranslationCacheFromCloudflare(language, apiKey);
const cacheFromCloudFlare = isCompressionSupported() ? await getTranslationCacheFromCloudflare(language, apiKey) : {};
window.translationCache[window.location.pathname][language] = {
...(window.translationCache?.[window.location.pathname]?.[language] || {}),
...cacheFromCloudFlare
Expand Down
8 changes: 7 additions & 1 deletion utils/compressions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function isCompressionSupported() {
const isSupported = window.CompressionStream && window.DecompressionStream && window.TextEncoder && window.TextDecoder;
return isSupported;
}

function compressToArrayBuffer(string, encoding) {
const byteArray = new TextEncoder().encode(string);
const cs = new CompressionStream(encoding);
Expand Down Expand Up @@ -60,5 +65,6 @@ module.exports = {
compressToArrayBuffer,
decompressArrayBuffer,
compressToString,
decompressString
decompressString,
isCompressionSupported
};
2 changes: 0 additions & 2 deletions utils/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const isBrowser = () => typeof window !== 'undefined'
const API_URL = "https://api.weploy.ai"
const CDN_URL = ""
const KV_URL = "https://cdn.weploy.ai"
const SHOULD_COMPRESS_PAYLOAD = true

/** Translation Options */
var weployOptions;
Expand Down Expand Up @@ -112,6 +111,5 @@ module.exports = {
API_URL,
CDN_URL,
KV_URL,
SHOULD_COMPRESS_PAYLOAD,
weployOptions
}
6 changes: 3 additions & 3 deletions utils/languages/fetchLanguageList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { decompressArrayBuffer } = require("../compressions");
const { isBrowser, API_URL, getWeployOptions, setWeployActiveLang, setWeployOptions, SHOULD_COMPRESS_PAYLOAD } = require("../configs");
const { decompressArrayBuffer, isCompressionSupported } = require("../compressions");
const { isBrowser, API_URL, getWeployOptions, setWeployActiveLang, setWeployOptions } = require("../configs");
const { renderWeploySelectorState } = require("../selector/renderWeploySelectorState");

async function fetchLanguageList(apiKey) {
Expand All @@ -9,7 +9,7 @@ async function fetchLanguageList(apiKey) {
if (window.weployError) return [];
return [];

const shouldCompressResponse = SHOULD_COMPRESS_PAYLOAD;
const shouldCompressResponse = isCompressionSupported();
const headers = {
"X-Api-Key": apiKey,
}
Expand Down
8 changes: 5 additions & 3 deletions utils/translation/getTranslationsFromAPI.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { compressToArrayBuffer, decompressArrayBuffer } = require("../compressions");
const { API_URL, setWeployActiveLang, isBrowser, SHOULD_COMPRESS_PAYLOAD } = require("../configs");
const { compressToArrayBuffer, decompressArrayBuffer, isCompressionSupported } = require("../compressions");
const { API_URL, setWeployActiveLang, isBrowser } = require("../configs");
const { renderWeploySelectorState } = require("../selector/renderWeploySelectorState");

async function getTranslationsFromAPI(strings, language, apiKey) {
Expand All @@ -23,7 +23,9 @@ async function getTranslationsFromAPI(strings, language, apiKey) {

const stringifiedPayload = JSON.stringify(finalPayload);

const shouldCompressPayload = SHOULD_COMPRESS_PAYLOAD;
const shouldCompressPayload = isCompressionSupported();
if (!shouldCompressPayload) console.log("WEPLOY: Compression is not supported in this browser, therefore the payload will be sent uncompressed.");

const compressedPayload = shouldCompressPayload ? await compressToArrayBuffer(stringifiedPayload, "gzip") : null;

return await new Promise((resolve) => {
Expand Down

0 comments on commit 5b6c1e5

Please sign in to comment.