-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4618 from Myestery/pwa-convert
remotion.dev/convert: Allow to use it offline
- Loading branch information
Showing
11 changed files
with
240 additions
and
10 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { RemixBrowser } from "@remix-run/react"; | ||
import { hydrateRoot } from "react-dom/client"; | ||
import { startTransition } from "react"; | ||
|
||
const registerServiceWorker = () => { | ||
if ("serviceWorker" in navigator) { | ||
window.addEventListener("load", () => { | ||
navigator.serviceWorker | ||
.register("/convert/service-worker.js") | ||
.catch((error) => { | ||
console.log("SW registration failed:", error); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
startTransition(() => { | ||
hydrateRoot(document, <RemixBrowser />); | ||
registerServiceWorker(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-nocheck | ||
const $FILES = []; | ||
// ^ leave this - will get filled in during build | ||
// --auto-generated-until-here | ||
|
||
const CACHE_NAME = 'remotion-convert-v1'; | ||
|
||
// Helper function to determine if a request is under /convert | ||
function isConvertPath(url) { | ||
return url.pathname.startsWith('/convert'); | ||
} | ||
|
||
self.addEventListener('install', (event) => { | ||
event.waitUntil( | ||
(async () => { | ||
const cache = await caches.open(CACHE_NAME); | ||
await cache.addAll($FILES); | ||
// @ts-expect-error no types | ||
await self.skipWaiting(); | ||
})(), | ||
); | ||
}); | ||
|
||
self.addEventListener('activate', (event) => { | ||
event.waitUntil( | ||
(async () => { | ||
// Clean up old caches | ||
const cacheNames = await caches.keys(); | ||
await Promise.all( | ||
cacheNames | ||
.filter((name) => name !== CACHE_NAME) | ||
.map((name) => caches.delete(name)), | ||
); | ||
// Take control of all pages immediately | ||
// @ts-expect-error no types | ||
await self.clients.claim(); | ||
})(), | ||
); | ||
}); | ||
|
||
self.addEventListener('fetch', (event) => { | ||
const url = new URL(event.request.url); | ||
|
||
if (!isConvertPath(url)) { | ||
return; | ||
} | ||
|
||
// Only handle same-origin requests | ||
if (!url.origin.includes(self.location.origin)) { | ||
return; | ||
} | ||
|
||
// If it is a file selected from the user's device, do not cache it | ||
if (url.protocol === 'file:') { | ||
return; | ||
} | ||
|
||
// Special handling for /convert paths | ||
if (isConvertPath(url)) { | ||
event.respondWith( | ||
(async () => { | ||
try { | ||
// Try to fetch the network request | ||
const response = await fetch(event.request); | ||
|
||
// Cache the new response | ||
const cache = await caches.open(CACHE_NAME); | ||
await cache.put(event.request, response.clone()); | ||
|
||
return response; | ||
} catch { | ||
// If network fails, try cache | ||
const cachedResponse = await caches.match(event.request); | ||
if (cachedResponse) { | ||
return cachedResponse; | ||
} | ||
|
||
// If both network and cache fail, return a basic offline response | ||
if (event.request.headers.get('accept')?.includes('text/html')) { | ||
return caches.match('/convert'); | ||
} | ||
|
||
return new Response('Network error happened', { | ||
status: 408, | ||
headers: {'Content-Type': 'text/plain'}, | ||
}); | ||
} | ||
})(), | ||
); | ||
return; | ||
} | ||
|
||
// For all other requests, do a simple network-first strategy | ||
event.respondWith( | ||
(async () => { | ||
try { | ||
return await fetch(event.request); | ||
} catch { | ||
return caches.match(event.request); | ||
} | ||
})(), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'fs'; | ||
const files = fs.readdirSync('spa-dist/client'); | ||
const assets = fs.readdirSync('spa-dist/client/assets'); | ||
const toCache = [ | ||
...files | ||
.filter((f) => { | ||
return fs.statSync(`spa-dist/client/${f}`).isFile(); | ||
}) | ||
.map((f) => `/convert/${f}`.replace('/index.html', '')), | ||
...assets | ||
.filter((f) => { | ||
if (!fs.statSync(`spa-dist/client/assets/${f}`).isFile()) { | ||
throw new Error('Unexpected output'); | ||
} | ||
return true; | ||
}) | ||
.map((f) => `/convert/assets/${f}`), | ||
]; | ||
|
||
const result = await Bun.build({ | ||
entrypoints: ['./app/service-worker.ts'], | ||
}); | ||
|
||
if (!result.success) { | ||
console.log(result.logs); | ||
throw new Error('Failed to build service worker'); | ||
} | ||
|
||
const firstOutput = result.outputs[0]; | ||
|
||
if (!firstOutput) { | ||
throw new Error('No output'); | ||
} | ||
const text = await firstOutput.text(); | ||
const replaced = '$FILES = [];'; | ||
if (!text.includes(replaced)) { | ||
throw new Error('Unexpected output'); | ||
} | ||
|
||
await Bun.write( | ||
'spa-dist/client/service-worker.js', | ||
text.replace(replaced, `$FILES = ${JSON.stringify(toCache)};`), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "Remotion Convert", | ||
"id": "com.remotion.convert", | ||
"short_name": "Remotion Convert", | ||
"description": "Convert videos using WebCodecs", | ||
"start_url": "/convert", | ||
"display": "standalone", | ||
"background_color": "#ffffff", | ||
"theme_color": "#0B84F3", | ||
"icons": [ | ||
{ | ||
"src": "/convert/pwa-icon-192.png", | ||
"sizes": "192x192", | ||
"type": "image/png" | ||
}, | ||
{ | ||
"src": "/convert/pwa-icon-512.png", | ||
"sizes": "512x512", | ||
"type": "image/png" | ||
}, | ||
{ | ||
"src": "/convert/pwa-icon-512.png", | ||
"sizes": "512x512", | ||
"type": "image/png", | ||
"purpose": "maskable" | ||
} | ||
] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters