Coder Social home page Coder Social logo

demo-eleventy-serverless's Introduction

Eleventy Serverless Demo

Running Eleventy inside of a Netlify serverless function.

Read the documentation on 11ty.dev

Run it

Locally

  1. Run npm install
  2. Run npm start
  3. Navigate to the demo URL at http://localhost:8080/

Production

  1. View the demo on Netlify
  2. Deploy your own to Netlify

How it works

Read the documentation on 11ty.dev

This requires Eleventy 1.0 Canary 30 or newer. Be careful here, Canary is considered unstable! Don’t use it in production.

  1. Use Eleventy as normal.
    • In this demo src is the input directory.
    • For this demo we include one Nunjucks template (./src/sample-nunjucks.njk), a Global Data file, an include template, and an Eleventy layout.
    • To make any template file into a serverless template, modify your permalink object to include a serverless key.
---
permalink:
  build: "/"
  serverless: "/:slug/"
---

This makes eleventy.path.slug (the slug name matches :slug) available in global data for use in your serverless templates.

  1. Add the bundler plugin to your Eleventy configuration file (probably .eleventy.js). The name you pass into the plugin (we use serverless in this example) should match the key inside of your template’s permalink object (permalink.serverless).
const { EleventyServerlessBundlerPlugin } = require("@11ty/eleventy");

module.exports = function(eleventyConfig) {
	eleventyConfig.addPlugin(EleventyServerlessBundlerPlugin, {
		name: "serverless",
		functionsDir: "./netlify/functions/",
	});
};
  1. ./netlify/functions/serverless/index.js is the boilerplate serverless function code. You’ll need to create this yourself.
const { EleventyServerless } = require("@11ty/eleventy");
const { builder } = require("@netlify/functions");

// For the bundler (auto-generated by the plugin)
require("./eleventy-bundler-modules.js");

async function handler (event) {
	let elev = new EleventyServerless("serverless", event.path, {
		inputDir: "src",
		functionsDir: "netlify/functions/",
		query: event.queryStringParameters,
	});

	try {
		return {
			statusCode: 200,
			headers: {
				"Content-Type": "text/html; charset=UTF-8"
			},
			body: await elev.render()
		};
	} catch (error) {
		return {
			statusCode: error.httpStatusCode || 500,
			body: JSON.stringify({
				error: error.message
			})
		};
	}
}

// Netlify On-demand Builder (runs on first request only)
exports.handler = builder(handler);
  1. Add entries to your .gitignore file so the bundles aren’t checked into your repository.
netlify/functions/serverless/**
!netlify/functions/serverless/index.js

demo-eleventy-serverless's People

Contributors

zachleat avatar

Watchers

Letra Studio 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.