The svelte-Kit project deplores packaged static files to the server, using adapter-static adapters and nginx agents.

version

"@ sveltejs/adapter - static", "^ 1.0.0 - next. 29", "@ sveltejs/kit" : "^ 1.0.0 - next. 295", "@ vitejs/plugin - legacy" : "^ 1.7.1",Copy the code

Pay attention to

Running the project after updating @sveltejs/kit will report an esBuild error.

Project directory

Packaged directory

Project configurationsvelte.config.js

import preprocess from "svelte-preprocess"; // Preprocessor
import adapter from '@sveltejs/adapter-static'; // Static adapter
import path from "path";

const subpackage = ['echarts'.'mo.umd']

const config = {
    kit: {
        adapter: adapter({
            pages: 'build'.// The directory to write the pre-rendered page to. It defaults to 'build'.
            assets: 'build'.// Resource package output directory includes svelte-kit output JS and CSS
            fallback: null // Specify a backup page for SPA mode, such as' index.html 'or' 200. HTML 'or' 404.html '.
        }),
        vite: () = > ({
            manifest: true.sourcemap: true.emptyOutDir: true.target: "es2018".overlay: false.resolve: {
                alias: {
                    $utils: path.resolve('./src/utils') // The alias used in the project}},server: {
                proxy: {
                    / / API agent
                    '/prod-api': {
                        target: 'http://xxx.com'.changeOrigin: true.rewrite: path= > path.replace(/ ^ /prod-api/, ' ')}}},build: {
                rollupOptions: {
                    output: {
                        manualChunks(id) {
                            if (id.includes('node_modules')) {
                                const hassSubpackage = subpackage.findIndex(val= >id.indexOf(val) ! = = -1)
                                if(hassSubpackage ! = = -1) {
                                    return id.toString().split('node_modules/') [1].split('/') [0].toString()
                                }
                                return 'vendor'}}}}}})},preprocess: [
        preprocess({
            postcss: true,})]};export default config;
Copy the code

Nginx configuration

Try_files $uri $uri /index.html;

Try_files $uri $uri/ /index.html; The index.svelte in the Adapter-static folder is generated into the parent directory from the packaged directory structure above. So you don’t have to look under the folder.

server {
    listen 80;
    server_name  xxx.com;

    location / {
        proxy_passhttps://xxx.com; }}server {
    listen 443 ssl;
    server_name  xxx.com;

    access_log /var/log/nginx/xxx_access.log;
    error_log /var/log/nginx/xxx_error.log;

    ssl_certificate conf.d/ssl/4678010__xxx.com.pem;
    ssl_certificate_key conf.d/ssl/4678010__xx.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphersECDHE-RSA-AES128-GCM-SHA256:HIGH:! aNULL:! MD5:! RC4:! DHE:! MD5;ssl_prefer_server_ciphers on;

    location / {
        root /data/html;
        try_files $uri $uri /index.html;
    }

    location /prod-api/ {
        proxy_passhttp://zsdn/; }}Copy the code