diff --git a/excalidraw-app/App.tsx b/excalidraw-app/App.tsx index 543bf05b6f..530b521b70 100644 --- a/excalidraw-app/App.tsx +++ b/excalidraw-app/App.tsx @@ -93,6 +93,7 @@ import { STORAGE_KEYS, SYNC_BROWSER_TABS_TIMEOUT, } from "./app_constants"; +import { APP_ROUTES } from "./app_routes"; import Collab, { collabAPIAtom, isCollaboratingAtom, @@ -1312,7 +1313,7 @@ const ExcalidrawWrapper = () => { const ExcalidrawApp = () => { const isCloudExportWindow = - window.location.pathname === "/excalidraw-plus-export"; + window.location.pathname === APP_ROUTES.excalidrawPlusExport; if (isCloudExportWindow) { return ; } diff --git a/excalidraw-app/app_routes.ts b/excalidraw-app/app_routes.ts new file mode 100644 index 0000000000..cbcede170d --- /dev/null +++ b/excalidraw-app/app_routes.ts @@ -0,0 +1,11 @@ +/** + * Paths, besides `/`, that the app is served on. + * + * Adding a path here lets the service worker serve the cached app for it + * (see `navigateFallbackAllowlist` in vite.config.mts). The host must serve + * the app for it too, or the path is a 404: add a matching rewrite to + * `/index.html` in the root vercel.json (tests/appRoutes.test.ts checks it). + */ +export const APP_ROUTES = { + excalidrawPlusExport: "/excalidraw-plus-export", +} as const; diff --git a/excalidraw-app/tests/appRoutes.test.ts b/excalidraw-app/tests/appRoutes.test.ts new file mode 100644 index 0000000000..bd8f5cd5e9 --- /dev/null +++ b/excalidraw-app/tests/appRoutes.test.ts @@ -0,0 +1,20 @@ +import fs from "fs"; +import path from "path"; + +import { APP_ROUTES } from "../app_routes"; + +describe("app routes", () => { + it("should each have a rewrite to the app in vercel.json, and vice versa", () => { + const vercelConfig: { + rewrites?: { source: string; destination: string }[]; + } = JSON.parse( + fs.readFileSync(path.resolve(__dirname, "../../vercel.json"), "utf8"), + ); + + const rewrittenToApp = (vercelConfig.rewrites ?? []) + .filter((rewrite) => rewrite.destination === "/index.html") + .map((rewrite) => rewrite.source); + + expect(rewrittenToApp.sort()).toEqual(Object.values(APP_ROUTES).sort()); + }); +}); diff --git a/excalidraw-app/vite.config.mts b/excalidraw-app/vite.config.mts index 0488bb1d3d..f7ddd0f724 100644 --- a/excalidraw-app/vite.config.mts +++ b/excalidraw-app/vite.config.mts @@ -9,6 +9,7 @@ import { createHtmlPlugin } from "vite-plugin-html"; import Sitemap from "vite-plugin-sitemap"; import { woff2BrowserPlugin } from "../scripts/woff2/woff2-vite-plugins"; +import { APP_ROUTES } from "./app_routes"; import { MERMAID_CHUNKS_LIST_NAME } from "./data/cacheMermaidChunks"; import type { Plugin, Rollup } from "vite"; @@ -274,6 +275,7 @@ export default defineConfig(({ mode }) => { Sitemap({ hostname: "https://excalidraw.com", outDir: "build", + exclude: ["/404"], changefreq: "monthly", // its static in public folder generateRobotsTxt: false, @@ -306,8 +308,19 @@ export default defineConfig(({ mode }) => { }, workbox: { + // Serve the cached app shell only for the paths the app lives on + // (matched against pathname + search). Anything else goes to the + // network, so that unknown paths get the server's 404. + // To add a path, add it to APP_ROUTES. + navigateFallbackAllowlist: [ + /^\/(\?|$)/, + ...Object.values(APP_ROUTES).map( + (route) => new RegExp(`^${route}(\\?|$)`), + ), + ], // don't precache fonts, locales and separate chunks globIgnores: [ + "404.html", "fonts.css", "**/locales/**", "service-worker.js", @@ -433,23 +446,6 @@ export default defineConfig(({ mode }) => { }, }, ], - share_target: { - action: "/web-share-target", - method: "POST", - enctype: "multipart/form-data", - params: { - files: [ - { - name: "file", - accept: [ - "application/vnd.excalidraw+json", - "application/json", - ".excalidraw", - ], - }, - ], - }, - }, screenshots: [ { src: "/screenshots/virtual-whiteboard.png", diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index 6f7ec057b1..bdf1b38b11 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -15,7 +15,6 @@ import { shouldRotateWithDiscreteAngle, isArrowKey, KEYS, - APP_NAME, CURSOR_TYPE, DEFAULT_STROKE_STREAMLINE, DEFAULT_STROKE_STREAMLINE_PRECISE, @@ -3895,16 +3894,7 @@ class App extends React.Component { this.resizeObserver?.observe(this.excalidrawContainerRef.current); } - const searchParams = new URLSearchParams( - this.ownerWindow.location.search.slice(1), - ); - - if (searchParams.has("web-share-target")) { - // Obtain a file that was shared via the Web Share Target API. - this.restoreFileFromShare(); - } else { - this.updateDOMRect(this.initializeScene); - } + this.updateDOMRect(this.initializeScene); // note that this check seems to always pass in localhost if (isBrave() && !isMeasureTextSupported()) { @@ -4920,27 +4910,6 @@ class App extends React.Component { }); } - restoreFileFromShare = async () => { - try { - const webShareTargetCache = await caches.open("web-share-target"); - - const response = await webShareTargetCache.match("shared-file"); - if (response) { - const blob = await response.blob(); - const file = new File([blob], blob.name || "", { type: blob.type }); - this.loadFileToCanvas(file, null); - await webShareTargetCache.delete("shared-file"); - this.ownerWindow.history.replaceState( - null, - APP_NAME, - this.ownerWindow.location.pathname, - ); - } - } catch (error: any) { - this.setState({ errorMessage: error.message }); - } - }; - /** * adds supplied files to existing files in the appState. * NOTE if file already exists in editor state, the file data is not updated diff --git a/public/404.html b/public/404.html new file mode 100644 index 0000000000..d064e992a9 --- /dev/null +++ b/public/404.html @@ -0,0 +1,351 @@ + + + + + Page not found | Excalidraw + + + + + + + + +
+ + +

+ + + + +

+

There is nothing at this address.

+ Open Excalidraw + +
+ + + + diff --git a/public/robots.txt b/public/robots.txt index f956a6b93c..fc99f2094d 100644 --- a/public/robots.txt +++ b/public/robots.txt @@ -1,13 +1,4 @@ -User-agent: Twitterbot -Disallow: - -User-agent: facebookexternalhit -Disallow: - User-agent: * -Allow: /$ Allow: / -Allow: /sitemap.xml -Disallow: / Sitemap: https://excalidraw.com/sitemap.xml diff --git a/vercel.json b/vercel.json index faa0838227..78f5fa9931 100644 --- a/vercel.json +++ b/vercel.json @@ -64,6 +64,14 @@ "destination": "https://marketplace.visualstudio.com/items?itemName=pomdtr.excalidraw-editor" } ], + "rewrites": [ + { + "source": "/excalidraw-plus-export", + "destination": "/index.html" + } + ], + "framework": null, + "buildCommand": "yarn build", "outputDirectory": "excalidraw-app/build", "installCommand": "yarn install" }