sveltejs / sveltejs/kit

Automatically disable +layout.server.ts and server.ts for static sites

Open
#12,013 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

I'm writing on code base to rule them all (Capacitor SPA (iOS + Android) & Web SSR)

I do the following in the config

import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
import adapterNode from "@sveltejs/adapter-node";
import adapterStatic from "@sveltejs/adapter-static";

// Determine the mode - this can be set as an environment variable or changed manually
const BUILD_MODE = process.env.BUILD_MODE || "SSR"; // default to SSR, change to 'SPA' for SPA mode

let adapterOptions = {};

// Switch depending on the mode
if (BUILD_MODE === "SPA") {
    adapterOptions = {
        pages: "build_spa",
        assets: "build_spa",
        strict: true,
        fallback: "index.html",
    };
} else {
    adapterOptions = {
        pages: "target/classes/static/",
        assets: "target/classes/static/",
        fallback: "index.html",
    };
}

const adapter = BUILD_MODE === "SSR" ? adapterNode(adapterOptions) : adapterStatic(adapterOptions);

/** @type {import('@sveltejs/kit').Config} */
const config = {
    // Consult https://kit.svelte.dev/docs/integrations#preprocessors
    // for more information about preprocessors
    preprocess: vitePreprocess(),

    kit: {
        adapter,
    },
};

export default config;

When I build the app for capacitor I have to manually rename the +server.layout files, then name them back safely.

#!/bin/bash

# Build the SvelteKit app for Mobile (SPA)

# Function to rename +layout.server.ts files to +layout.server.ts.bak
rename_layout_files() {
    find $SRC_DIR -type f -name "+layout.server.ts" -exec mv {} {}.bak \;
}

# Function to rename +layout.server.ts.bak files back to +layout.server.ts
restore_layout_files() {
    find $SRC_DIR -type f -name "+layout.server.ts.bak" -exec sh -c 'mv "$0" "${0%.bak}"' {} \;
}

# Trap to ensure that restore_layout_files is called if the script exits early due to an error
trap 'restore_layout_files' EXIT

# Abort on any error (including if find or npm run fails)
set -e

# Set env
export PUBLIC_DEV=TRUE
export BUILD_MODE=SPA

SRC_DIR="./src"

# Rename +layout.server.ts files before the build
rename_layout_files

vite build --mode production --base

# Rename +layout.server.ts files back after successful build
# The restore_layout_files function will also be called here due to the trap if the script hasn't exited early
restore_layout_files

npx cap sync
# npx cap open ios
# npx cap open android

# Clear the trap
trap - EXIT
 

It would be nice if there was some config to do this automatically and it would also be nice to have __data.json explained in the docs.

Reproduction

It's mentioned in the description.

Logs

No response

System Info
System:
    OS: macOS 14.3.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 333.03 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.6.2 - /opt/homebrew/bin/node
    npm: 10.2.4 - /opt/homebrew/bin/npm
    pnpm: 8.6.12 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 120.0.6099.71
    Safari: 17.3.1
Severity

annoyance

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SvelteKit config that switches between adapterNode and adapterStatic, then trace how +layout.server.ts and +server.ts are handled during static builds. Define what static mode should disable, verify the Capacitor build without renaming files, and explain __data.json in the documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
build-system, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.