Code Monkey home page Code Monkey logo

svelte-aio's Introduction

svelte-aio

NPM

Automatic image optimization for SvelteKit, inspired by NextJS

This library are still under construction, many bugs and missing features. Feel free to contribute and making bugs report.

Table of contents

  1. Usage
  2. Configuration

Usage

Check out full sample at src/routes

Install dependencies

pnpm add svelte-aio

In routes/api/_images, create +server.ts endpoint

import { requestHandler } from 'svelte-aio/api'

import type { RequestHandler } from '@sveltejs/kit'

export const GET: RequestHandler = requestHandler()

Then use normally (almost) like next/image

<!-- +page.ts -->
<script lang="ts">
  import Image from 'svelte-aio'
</script>

<Image
  src="https://demo.rayriffy.com/tom-scott.jpg"
  width={801}
  height={801}
  alt="Tom Scott"
  class="rounded-xl shadow-md"
/>

Configuration

Server configrations can be specified via option params. All parameters are optional!

export const GET: RequestHandler = requestHandler({
  avif: false,
  remoteDomains: ['demo.rayriffy.com'],
  allowedDomains: ['svelte-aio.vercel.app'],
  ttl: 1000 * 60 * 60 * 24 * 7,
  storePath: '.svelte-kit/images',
})

avif

boolean

Enable AVIF image format. Defaults to false

Warning: optimizing image into AVIF format currently not reccomended due to high CPU and memory usage. Overall performance is not great when comparing to WebP.

remoteDomains

string[] | undefined

List of domains that API will be allowed to optimize. Defaults to unset

From example above, remoteDomains: ['demo.rayriffy.com'], means that API will only be allowed to optimize images that served from demo.rayriffy.com.

Unset this option will tell API to optimize ALL IMAGES from EVERYWHERE

allowedDomains

string[] | undefined

List of domains that allowed to use the API, this will be checked via header Referer

Only affects on NODE_ENV=production. Unset this option will allow anywhere to request image from this API.

ttl

number

Time until images will become invalidated, defaults to 7 days

Values are in milliseconds

storePath

string

Directory path to cache optimized images. Defaults to .svelte-kit/images

Provided paths will be relative to process.cwd()

svelte-aio's People

Contributors

rayriffy avatar cubedhuang avatar

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.