Skip to content

A frontend for managing an SVG spritesheet, using an AWS S3 bucket.

License

Notifications You must be signed in to change notification settings

n8jadams/aws-s3-svg-cms

Repository files navigation

AWS S3 SVG CMS

A frontend for managing an SVG spritesheet, using an AWS S3 bucket.

(NEW: Comes with a development experience that mocks the S3 API locally)

AWS S3 SVG CMS Screenshot

Prerequisites

A modern version of Node

Setup

Development - Using localhost and the filesystem

  1. Clone this repo
  2. Duplicate the .env.example and rename it .env
$ cp .env.example .env
  1. Fill out the all of the .env variables. It doesn't matter what they are, except SPRITE_SHEET_FILENAME_WITHOUT_EXT, this will be the name of the compiled sprite sheet. (Exclude .svg.)
  2. Install, run the dev script
$ npm install
$ npm run dev
  1. Open up http://localhost:4000 and verify changes and compiled sprite sheet in the .dev directory!

Production - Using an AWS S3 bucket

  1. Clone this repo
  2. Duplicate the .env.example and rename it .env.
$ cp .env.example .env
  1. Fill out the .env variable for SPRITE_SHEET_FILENAME_WITHOUT_EXT. (As indicated, don't include .svg or any extension.)
  2. In the AWS S3 console, create a bucket. Add the name of the bucket to the S3_BUCKET_NAME variable in your .env as well as the AWS_DEFAULT_REGION. You probably want to this bucket to have public read access.
  3. In your S3 bucket, create folders named compiled-svg-sheet and source-svgs
  4. (Optional, if you haven't set your ~/.aws/config or if your default isn't the user you want), In the AWS IAM console, grab your credentials and set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in your .env. Any user that has read/write permissions on the S3 bucket will do.
  5. Install, build, and start the app.
$ npm install
$ npm run build
$ npm start
  1. Open up http://localhost:4000
  2. After making changes, the compiled sprite sheet will be accessible at the following url:
https://{S3_BUCKET_NAME}.s3-{AWS_DEFAULT_REGION}.amazonaws.com/compiled-svg-sheet/{SPRITE_SHEET_FILENAME_WITHOUT_EXT}.svg

Using the SVG Sprite Sheet

Inject it into your page with javascript like so:

window.addEventListener('load', () => {
	const svgUrl = `https://${S3_BUCKET_NAME}.s3-${AWS_DEFAULT_REGION}.amazonaws.com/compiled-svg-sheet/${SPRITE_SHEET_FILENAME_WITHOUT_EXT}.svg`

	fetch(svgUrl)
		.then((res) => {
			if (!res.ok) {
				throw new Error(res.statusText)
			}
			return res.text()
		})
		.then((svg) => {
			const div = document.createElement('div')
			div.innerHTML = svg
			document.body.insertBefore(div, document.body.childNodes[0])
		})
		.catch((e) => {
			throw new Error(e)
		})
})

And then add individual svgs in your html!

<div class="some-container-class">
	<svg>
		<use xlink:href="#some-svg-id"></use>
	</svg>
</div>

About

A frontend for managing an SVG spritesheet, using an AWS S3 bucket.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published