Giter Club home page Giter Club logo

svelte-adapter-appengine's Introduction

svelte-adapter-appengine

Easily deploy your SvelteKit applications on Google Cloud App Engine with the svelte-adapter-appengine package.

npm Tests SvelteKit

Getting Started

To set up the adapter in your SvelteKit project:

  1. Install the package as a development dependency:
npm install --save-dev svelte-adapter-appengine
  1. Update your svelte.config.js to use the adapter:
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
+import adapter from "svelte-adapter-appengine";

/** @type {import('@sveltejs/kit').Config} */
const config = {
  preprocess: vitePreprocess(),

  kit: {
+   adapter: adapter(),
  },
};

export default config;
  1. Build your application:
npm run build
  1. Deploy your application to App Engine:
gcloud app deploy --project <CLOUD_PROJECT_ID> build/app.yaml

Learn more about the gcloud utility in the official documentation

Configuration Options

Customize the adapter behavior using the following options:

adapter({
  // Build output directory (default: `/build`)
  out: "/build",

  // Enable Google Cloud Tracing Agent for improved logging (default: `false`)
  useCloudTracing: false,

  // Enable or disable Google Cloud Logging (default: `false`)
  // See: https://cloud.google.com/logging/docs/overview
  useCloudLogging: false,

  // Specify external modules for the esbuild step
  external: [],

  // Specify Node modules to be added to the `package.json` file in the build step
  // These modules will be fetched when the application is deployed
  dependencies: [],

  // Set the Node.js version for the App Engine runtime (default: `18`)
  // See available runtimes: https://cloud.google.com/appengine/docs/standard/nodejs/runtime
  nodejsRuntime: 18,
});

You can also customize the generated app.yaml file by creating an app.yaml file in your project root. The adapter will merge your custom configuration with the generated app.yaml, allowing you to define custom machine types, routes, or other app.yaml configurations.

Adapter Output

The Server-Side Rendering (SSR) part of SvelteKit is hosted on App Engine using a Node.js runtime, using polka to mimic @sveltejs/adapter-node .

Static files are served directly from Cloud Storage, bypassing the Node.js web server. The adapter automatically generates routes for all static assets in the app.yaml file.

Example Application

Check out a live example application at https://svelte-adapter-demo.uc.r.appspot.com/. This demo app is the default SvelteKit template deployed with the default adapter settings.

svelte-adapter-appengine's People

Contributors

aarya-a-patel avatar abynim avatar dependabot[bot] avatar github-actions[bot] avatar halfdanj avatar sfriedel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

svelte-adapter-appengine's Issues

List of `entries` for prerendering is ignored

Currently doing some detective work around this but:

  • I have a list of urls to prerender in kit.prerender.entries within my svelte.config.js. These do not prerender.
  • I also have some pages with export const prerender = true in their +page.ts. These do prerender.
  • crawl is set to false.

My suspicion is that this may be an issue with the adapter API, rather than with this adapter, but it's highly frustrating.

Unexpected token

Upon adding the svelte-adapter-appengine I get the following error on build using strict typescript with lang="ts" in script tags.

<script lang="ts">
 2 |    import AuthHeader from '$lib/AuthHeader.svelte'
 3 |    import type { PageData } from './$types'
                    ^
 4 |
 5 |    export let data: PageData
error during build:
ParseError: Unexpected token
    at error (file:///C:/gocode/src/Setyr/Web/node_modules/svelte/compiler.mjs:16669:19)
    at Parser$1.error (file:///C:/gocode/src/Setyr/Web/node_modules/svelte/compiler.mjs:16747:9)
    at Parser$1.acorn_error (file:///C:/gocode/src/Setyr/Web/node_modules/svelte/compiler.mjs:16741:14)
    at Object.read_script [as read] (file:///C:/gocode/src/Setyr/Web/node_modules/svelte/compiler.mjs:8793:16)
    at tag (file:///C:/gocode/src/Setyr/Web/node_modules/svelte/compiler.mjs:15695:33)
    at new Parser$1 (file:///C:/gocode/src/Setyr/Web/node_modules/svelte/compiler.mjs:16706:21)
    at parse$I (file:///C:/gocode/src/Setyr/Web/node_modules/svelte/compiler.mjs:16838:20)
    at compile (file:///C:/gocode/src/Setyr/Web/node_modules/svelte/compiler.mjs:46610:17)
    at compileSvelte2 (file:///C:/gocode/src/Setyr/Web/node_modules/@sveltejs/vite-plugin-svelte/dist/index.js:466:22)
    at async Object.transform (file:///C:/gocode/src/Setyr/Web/node_modules/@sveltejs/vite-plugin-svelte/dist/index.js:2202:25)

svelte.config
import adapter from '@sveltejs/adapter-auto'
import { sveltekit, vitePreprocess } from '@sveltejs/kit/vite'

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

	kit: {
		adapter: adapter()
	}
}

export default config

// import appengine from 'svelte-adapter-appengine'

// /** @type {import('@sveltejs/kit').Config} */
// export default {
// 	kit: {
// 		adapter: appengine()
// 	}
// }

With the default config everything works as expected.
Commenting out the "export default config" and uncommenting the adapter produces the error
FYI adding the "target" per the docs throws an exception

Pre-rendered server-side routes don't have handlers generated for them.

I have an endpoint (src/routes/foo/[...slug].json/+server.ts) that I prerender (for $reasons), by passing the paths into the list of paths to prerender in svelte.config.js, and adding export const prerender = true to the endpoint file.

This correctly outputs a pile of files as build/storage/foo/**/*.json. However! It does not add those entries to the configured app.yaml, meaning they don't get uploaded/handled correctly by GAE.

I think this might be also generating entries for items in builder.prerendered.paths... but haven't had the time to look into this just yet.

Invalid runtime

After successful file upload, the gcloud app deploy step exits with the following error:

ERROR: (gcloud.app.deploy) Error Response: [3] "runtime: nodejs16" does not exist.

After checking the configuration of previous deployments using the "node" adapter, I can see that their runtime name was simply "nodejs".
Is any configuration inside gcloud required in order to use the specified "nodejs16" value generated into the app.yaml?

Site fails with errors saying: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain"...

"... Strict MIME type checking is enforced for module scripts per HTML spec."

When deploying files to App Engine with the help of this adapter, the javascript files that are uploaded to Google Cloud Storage (GCS) end up with the mime type text/plain (instead of "text/javascript" or perhaps "application/javascript"). This breaks my app and I can't figure out how to fix this, except by manually changing the metadata on every file within the corresponding GCS bucket. Any ideas?

Root path no longer prerendered following Vite 4 upgrade

As of sveltejs/[email protected], SvelteKit is using Vite 4.

I'm prerendering my root path; however, when I look in build/, index.html is no longer present. (This was working on prior versions of SK). There are various issues floating around related to paths.base and Vite, so I'm going to dive into the adapter in due course to see what's going on. It might be an API change that's effecting it. This issue is mainly me noting it out loud for now.

Static files in the app.yaml when building

Under the source directory in my Svelte project, I have a "static" folder containing images. Build adds the following lines to the app.yaml for each image file. These break the GCP deployment. By deleting these lines everything works as expected.
url: /fifthimage.jpg
static_files: storage\fifthimage.jpg
upload: storage\fifthimage.jpg
secure: always

Can not npm run build without manually saving 'polka' and 'compression' to package.json

Steps to reproduce:

  1. Using pnpm run pnpm install --save-dev svelte-adapter-appengine
  2. Add the adapter to svelte.config.js per the instructions
  3. npm run build. The following error messages are received.

> .svelte-kit/appengine-tmp/entry.js:1:18: error: Could not resolve "polka" (mark it as external to exclude it from the bundle)
> .svelte-kit/appengine-tmp/entry.js:2:24: error: Could not resolve "compression" (mark it as external to exclude it from the bundle)

I was able to get around this by just running pnpm install --save-dev polka compression and it worked fine, but It does increase my package size and it's likely worth documenting/looking into.

Thanks for writing this though I really wasn't looking forward to tackling this myself :).

Copy package-lock.json too

Currently, AppEngine complains during upload:

Updating service [default]...⠶WARNING: *** Improve build performance by generating and committing package-lock.json.

If this is not some kind of config error on my part, I'll be happy to submit a PR for this.

Demo app broken when using the adapter with SvelteKit >= 1.0.0-next.356

While investigating why the integration tests in #29 continued to fail I saw that the /todos endpoint of the demo app is broken when using the svelte-adapter-appengine on SvelteKit versions >= 1.0.0-next.356.

Steps to reproduce:

yes "" | npm init svelte@next example-app
cd example-app
npm install
npm install --save-dev svelte-adapter-appengine
sed -i 's/@sveltejs\/adapter-auto/svelte-adapter-appengine/' svelte.config.js
npm run build
node .appengine_build_output/index.js

Afterwards open http://localhost:8080/todos in the browser. The browser will try to load the page but it will never complete. I tested this with Chrome and Firefox on Linux. Interestingly when using curl to fetch the same URL the response arrives immediately.

I also tried the same using the official SvelteKit node adapter but with the node adapter the page loads fine.

From the behavior I suspect the issue may be related to sveltejs/kit#5291 but I'm not exactly sure why this would lead to problems with the adapter.

MIME type error for deployed online version, not locally

Even though running vite dev and also vite preview work locally, after deployment both chrome and safari aren't loading the JS due to this error (this is the chrome version, safari uses different wording):

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

I'm not sure whether this is due to a change in the adapter or in sveltekit.

This occurs with adapter version 0.9.1 and sveltekit build 504 - I tried downgrading to 0.9.0 and 477, but the behavior is the same (also, undoing all code changes done in the project since using these older versions does not make a difference).

installFetch moved/renamed in sveltekit>=341

Builds on the latest sveltekit release return this error:

.svelte-kit/appengine-tmp/entry.js:2:9: error: No matching export in "node_modules/@sveltejs/kit/dist/node/polyfills.js" for import "installFetch"

Sveltekit 341 renamed and moved installFetch. I think the fix is just to use installPolyfills from @sveltejs/kit/node/polyfills instead of installFetch.

Questions?

This is not really an issue, but a question and/or a feature request (depending on the answer!)

  1. Is there a better way to ask questions? Please alert me!

  2. Does the adaptor support API endpoints? I created a simple one, but it doesn't seem to work once deployed.

  3. Is there more documentation that I'm missing somewhere?

thanks,
-Steve

App defaults http instead of https on App Engine

Despite having the secure:always , the app resolves to use the unsecure http rather than https .

Although adding secure to all the urls fixes it . As shown below :

`handlers:

  • url: //?$
    secure: always
    static_files: storage/index.html
    upload: storage/index.html
  • url: /about/?$
    secure: always
    static_files: storage/about.html
    upload: storage/about.html
  • url: /_app/immutable/
    secure: always
    static_dir: storage/_app/immutable
    expiration: 30d 0h
  • url: /_app/
    secure: always
    static_dir: storage/_app
  • url: /.*
    secure: always
    script: auto`

Support nodejs18

Hi there,

As Google has announced the general availability of nodejs18 recently it would be great to be able to configure which runtime the adapter uses rather than have it hardcoded.

Perhaps the we could pass this in via the svelte.config.js file as an optional when we instantiate the adapter, i.e.

adapter: appengine({
runtime: 'nodejs18'
})

Kind regards,
Matt

SvelteKit 2.0 migration compatability

Does the adapter should support SvelteKit 2.0? Because I am getting the resolve dependency error with npm i or npm upgrade after applying npx svelte-migrate@latest sveltekit-2 migration script.

npm upgrade
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: @sveltejs/[email protected]
npm ERR! node_modules/@sveltejs/kit
npm ERR!   dev @sveltejs/kit@"^2.0.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @sveltejs/kit@"^1.0.0" from [email protected]
npm ERR! node_modules/svelte-adapter-appengine
npm ERR!   dev svelte-adapter-appengine@"^0.10.3" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Failed to fetch dynamically imported module _app/undefined

Once deployed as an app to google cloud, I get this error on page load, or on refreshing the browser.

Failed to fetch dynamically imported module: https://xxxxx.ew.r.appspot.com/_app/undefined

it seems to happen as a result of an extra item inserted into the nodes array - import("/_app/undefined") when hydrating a page as below

image

Seems to relate to a similar issue across other cloud providers (see issue with vercel below)

sveltejs/kit#3308

As soon as I use page links, ie. by clicking on links within the pages (not refreshing the browser) everything functions and navigates as expected.

Support sveltekit 1.0

With the recent announcement of 1.0 it would be great to get this in alignment.

"peerDependencies": {
"@sveltejs/kit": ">=1.0.0-next.341"
},

Thanks in advance,
Matt

Pass `app.yaml` config to the adapter

I intend on making a PR to implement this, but figured an issue first for thoughts would make sense.

I'm using this for a project, and I need to create an extra script to run after building to add some extra stuff to the app.yaml - some of these things are variable like env_variables, or service (dev, staging or prod).

I think it would be useful for the adapter to accept another argument, which will be dumped as yaml into the resulting app.yaml

kit: {
  adapter: appengine('build', [], {
    service:  process.env.SERVICE ,
    instance_class: "F4",
    automatic_scaling: {
      min_instances: 1
    },
    inbound_services: [ "warmup" ]
  })
}

"Invalid request body" error when running on Google App Engine

When running the app on Google App Engine, I have the error "Invalid Request Body".
I am using the last version of Sveltekit.

The log for the build :

[email protected] build
vite build

vite v3.1.0 building for production...
✓ 30 modules transformed.
vite v3.1.0 building SSR bundle for production...
✓ 44 modules transformed.
Generated an empty chunk: "hooks"
.svelte-kit/output/server/vite-manifest.json 1.29 KiB
.svelte-kit/output/server/index.js 67.80 KiB
.svelte-kit/output/server/entries/endpoints/api/notifications/_server.ts.js 0.07 KiB
.svelte-kit/output/server/entries/fallbacks/layout.svelte.js 0.23 KiB
.svelte-kit/output/server/entries/fallbacks/error.svelte.js 1.50 KiB
.svelte-kit/output/server/entries/pages/_page.svelte.js 0.31 KiB
.svelte-kit/output/server/chunks/index.js 3.10 KiB
.svelte-kit/output/server/chunks/hooks.js 0.00 KiB

Run npm run preview to preview your production build locally.
.svelte-kit/output/client/vite-manifest.json 2.21 KiB
.svelte-kit/output/client/_app/immutable/components/layout.svelte-34b45f3a.js 0.53 KiB / gzip: 0.35 KiB
.svelte-kit/output/client/_app/immutable/components/error.svelte-ea5211a3.js 2.07 KiB / gzip: 0.95 KiB
.svelte-kit/output/client/_app/immutable/components/pages/_page.svelte-9e7c726b.js 0.81 KiB / gzip: 0.47 KiB
.svelte-kit/output/client/_app/immutable/chunks/singletons-d21fa692.js 1.81 KiB / gzip: 0.99 KiB
.svelte-kit/output/client/_app/immutable/start-b228e340.js 22.92 KiB / gzip: 8.99 KiB
.svelte-kit/output/client/_app/immutable/chunks/0-3f4b2ff7.js 0.09 KiB / gzip: 0.09 KiB
.svelte-kit/output/client/_app/immutable/chunks/1-253c57f3.js 0.09 KiB / gzip: 0.09 KiB
.svelte-kit/output/client/_app/immutable/chunks/2-fdf0651a.js 0.09 KiB / gzip: 0.10 KiB
.svelte-kit/output/client/_app/immutable/chunks/index-4f5b2732.js 6.62 KiB / gzip: 2.70 KiB

And the log of the deployment :

Services to deploy:

descriptor: [/home/virgile/Projets/bodela/sources/motard_way/backoffice/build/app.yaml]
source: [/home/virgile/Projets/bodela/sources/motard_way/backoffice/build]
target project: [motard-book]
target service: [default]
target version: [20220910t113338]
target url: [https://motard-book.nw.r.appspot.com]
target service account: [App Engine default service account]

Do you want to continue (Y/n)? Y

Beginning deployment of service [default]...
Created .gcloudignore file. See gcloud topic gcloudignore for details.
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 9 files to Google Cloud Storage ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
Updating service [default]...⠛WARNING: *** Improve build performance by generating and committing package-lock.json.

Updating service [default]...done.
Setting traffic split for service [default]...done.
Deployed service [default] to [https://motard-book.nw.r.appspot.com]

You can stream logs from the command line by running:
$ gcloud app logs tail -s default

To view your application in the web browser run:
$ gcloud app browse

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.