Setting Up Scratch Assets for Offline Use with PlottyBot - CodeClubLuxembourg/plottybot-toolkit-web GitHub Wiki
Overview
This guide explains how to configure Scratch for offline environments with PlottyBot, an XY plotter controlled by a Raspberry Pi. The Scratch server is hosted locally on the Raspberry Pi, but assets are usually fetched from Scratch's public CDN. To ensure functionality without internet access, we will:
- Download assets locally.
- Serve assets from the Raspberry Pi.
- Modify the Scratch setup to use local assets dynamically.
Steps to Serve Local Assets
1. Replace the Default CDN with a Local URL
Modify the scratch-storage
dependency to prioritize local assets.
-
Code Change: Replace the default CDN URL in
node_modules/scratch-storage/src/Storage.js
with the following logic:if (window.location.hostname) { const baseUrl = window.location.origin; localCdn = `${baseUrl}/assets/`; } const defaultCdn = 'https://cdn.assets.scratch.mit.edu/internalapi/asset/';
This ensures Scratch fetches assets from
http://x.x.x.x/assets/
(hosted on the Raspberry Pi) if available, and falls back to the public CDN otherwise.
2. Download Assets Locally
To build a local repository of Scratch assets:
- Tool: Use Stilmant/scratch-asset-utils to download assets from the CDN.
3. Serve Assets on the Raspberry Pi
Use the same server hosting Scratch to serve the assets.
-
Directory Setup: Place the assets in
/assets/
within the server's document root. Example:/var/www/html/assets/
. -
Configuration:
- Assets will now be available at
http://x.x.x.x/assets/
.
- Assets will now be available at
Important Notes
- Modifying
scratch-storage
: Sincescratch-storage
is a dependency, modifications to its code require accessing the Scratch project's source code or patching the installed module directly. - Dynamic Asset URL: The dynamic
window.location.origin
ensures the Scratch setup works regardless of the Raspberry Pi's IP address. - Future Updates: Periodically update your local assets repository by re-running the asset download tool.
scratch-storage
How to Modify node_modules
Option 1: Modify in You can directly edit the installed module in node_modules
(useful for quick testing):
- Navigate to
node_modules/scratch-storage/
. - Edit the relevant file (e.g.,
src/Storage.js
) to adjust asset fetching logic. - Be aware that changes in
node_modules
will be overwritten if dependencies are reinstalled.
Option 2: Clone and Modify the Source
To make permanent or reusable changes:
-
Clone the repository:
git clone https://github.com/LLK/scratch-storage.git
-
Modify the source code (e.g., adjust the default CDN URL in
src/Storage.js
). -
Build the module (optional, if used in production):
npm install npm run build
-
Replace the original dependency with your modified version:
- Replace
scratch-storage
innode_modules
with your build. - Alternatively, use
npm link
to reference your local version during development.
- Replace
Option 3: Fork and Publish
-
Fork the
scratch-storage
repository on GitHub. -
Make your modifications.
-
Publish your forked version to npm under a new name (e.g.,
scratch-storage-local
):npm publish --access public
-
Update the
scratch-vm
orscratch-gui
package.json
to use your custom module:"scratch-storage": "your-npm-username/scratch-storage-local"