Svelte Demo
Display interactive web content
Frontend libraries like Svelte, React, or Observable Framework generate static webapps, but require a build step.
You generally have the following options to create a static HTML space.
Here, we propose a hack with two spaces:
In this tutorial, we deploy the default Svelte demo page.
Thanks to @XciD for the idea and optimizations to the Dockerfile. Thanks to @Wauplin for the review and corrections ๐.
Let's start by creating the site space that will host the built website. Go to https://huggingface.co/new-space. We call the space "svelte-demo" and select the "Static" SDK and the "Blank" Static template.
The space contains a README.md and default HTML and CSS files. See the initial state at: https://huggingface.co/spaces/severo/svelte-demo/tree/777cd50e4088a5deed54117d1ae6b561948db994
We don't need to work on this space anymore. It will be fully controlled from code space.
Let's now create another space that will contain the website's source code and the Dockerfile for building and deploying it.
We go to https://huggingface.co/new-space, call the space "build-svelte-demo," select the "Docker" SDK and then the "Blank" Docker template.
We work locally by cloning this space to our machine:
$ git clone git@hf.co:spaces/severo/build-svelte-demo
$ cd build-svelte-demo
As an example, let's create the Svelte demo page. We call vite and select Svelte as the framework with the Typescript variant.
$ npm init vite
Need to install the following packages:
create-vite@5.4.0
Ok to proceed? (y) y
> npx
> create-vite
โ Project name: โฆ svelte-demo
โ Select a framework: โบ Svelte
โ Select a variant: โบ TypeScript
Scaffolding project in /home/slesage/hf/build-svelte-demo/svelte-demo...
Done. Now run:
cd svelte-demo
npm install
npm run dev
You can also use Vite to scaffold projects for vanilla javascript, other libraries such as Vue or React, or use a community template.
Install the packages and start the development web server:
$ cd svelte-demo
$ npm install
$ npm run dev
Open the browser at http://localhost:5173. You should see the default Svelte page.
Copy the following code to a new file called Dockerfile at the repository's root.
FROM ubuntu:22.04
# Install Node.js 22 - https://github.com/nodesource/distributions?tab=readme-ov-file#installation-instructions-deb
# And python3
RUN apt update \
&& apt install -y curl python3 python3-pip \
&& rm -rf /var/lib/apt/lists/*
RUN curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh \
&& bash nodesource_setup.sh \
&& apt update \
&& apt install -y nodejs \
&& rm -rf /var/lib/apt/lists/* \
&& rm nodesource_setup.sh
RUN pip install --upgrade "huggingface_hub[cli]"
# Build the app
WORKDIR /tmp/app
COPY svelte-demo/ ./
RUN npm ci && npm rebuild && npm run build
# The site space name must be passed as an environment variable
# https://huggingface.co/docs/hub/spaces-sdks-docker#buildtime
ARG STATIC_SPACE
# The Hugging Face token must be passed as a secret (https://huggingface.co/docs/hub/spaces-sdks-docker#buildtime)
# 1. get README.md from the site space
RUN --mount=type=secret,id=HF_TOKEN,mode=0444,required=true \
huggingface-cli download --token=$(cat /run/secrets/HF_TOKEN) --repo-type=space --local-dir=/tmp/app/dist $STATIC_SPACE README.md && rm -rf /tmp/app/dist/.cache
# 2. upload the new build to the site space, including README.md
RUN --mount=type=secret,id=HF_TOKEN,mode=0444,required=true \
huggingface-cli upload --token=$(cat /run/secrets/HF_TOKEN) --repo-type=space $STATIC_SPACE /tmp/app/dist . --delete "*"
# Halt execution because the code space is not meant to run.
RUN exit 1
Some explanations:
The website is now deployed on the site space: https://huggingface.co/spaces/severo/svelte-demo
Let's recap the logic:
Now it's your turn. Share your creations!
Display interactive web content