Git Product home page Git Product logo

lambda-js-prerender's Introduction

lambda-js-prerender

git Version Build Status

AWS CloudFront Lambda serverless JavaScript prerenderer.

Features

  • Ability to prerender SPA (Single-page application) pages for SEO indexing.
  • Ability to create screenshots of dynamically generated pages for testing.
  • Serverless function, scales on a tight budget.
  • Can be set-up easily (in minutes).

Dependencies

Get the Chromium binary

$ wget -c https://github.com/nuxy/chromium-lambda-build/releases/download/0.0.2/headless_shell.tar.gz -O - | tar -xz

Deploying to AWS

$ ./deploy --profile <AWS credentials profile>

The following operations are orchestrated by AWS CloudFormation during execution:

  • Docker container image is created and uploaded to AWS Elastic Container Registry.
  • AWS Lambda function is created with a configured Function URL endpoint.
  • AWS CloudFront distribution is created using the new function as origin.
  • Network routing occurs thereby exposing your Lambda function URL

Invoking the service

Command-line

curl -X 'POST' \
  'https://<url-id>.lambda-url.<region>.on.aws' \
  -H 'Accept: text/html' \
  -H 'Content-Type: application/json' \
  -d '{"url": "<site-url>", "image": <boolean>}'

In Node.js

AWS SDK for JavaScript v3

const {LambdaClient, InvokeCommand} = require('@aws-sdk/client-lambda');

const client = new LambdaClient({region: '<region>'});

const params = {
  FunctionName: 'PrerenderApi',
  InvocationType: 'RequestResponse',
  LogType: 'Tail',
  Payload: JSON.stringify({
    url: '<site-url>',
    image: <boolean>
  })
};

const command = new InvokeCommand(params);

try {
  const {PayLoad} = await client.send(command);

  console.log(Payload?.body));

} catch (err) {
  console.warn(err.message);
  throw err;
}

AWS SDK for JavaScript v2

const AWS = require('aws-sdk');

const client = new AWS.Lambda({region: '<region>'});

const params = {
  FunctionName: 'PrerenderApi',
  InvocationType: 'RequestResponse',
  LogType: 'Tail',
  Payload: JSON.stringify({
    url: '<site-url>',
    image: <boolean>
  })
};

client.invoke(params).promise()
  .then(function({Payload}) {
    console.log(Payload?.body));
  })
  .catch(function(err) {
    console.warn(err.message);
    throw err;
  });

Environment variables

The following function environment overrides can be configured in the SAM template file:

Variable name Description Default value
PRERENDER_DEBUG Enable verbose logging to CloudWatch group false
PRERENDER_TIMEOUT Execution timeout in seconds 60

AWS requirements

In order to successfully deploy your application you must have set-up your AWS Config and have created an IAM user with the following policies:

WARNING: The policies above are provided to ensure a successful application deployment. It is recommended that you adjust these policies to meet the security requirements of your Lambda application. They should NOT be used in a Production environment.

Developers

CLI options

Run ESLint on project sources:

$ npm run lint

Generate Swagger OpenAPI definitions:

$ npm run genapi

Run Mocha unit tests:

$ npm run test

Performance

Running in Lambda there is overhead that occurs (cold start) when the environment is first launched. This overhead does not include headless shell initialization which creates its own latency. Due to this, you can expect a longer response time for the first request. As long as there is an active Lambda handler (hot start) all subsequent requests will not incurr this overhead.

References

Versioning

This package is maintained under the Semantic Versioning guidelines.

License and Warranty

This package is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.

lambda-js-prerender is provided under the terms of the MIT license

AWS is a registered trademark of Amazon Web Services, Inc.

Author

Marc S. Brooks

lambda-js-prerender's People

Contributors

nuxy avatar

Stargazers

Dylan Bishop avatar

Watchers

 avatar  avatar

lambda-js-prerender's Issues

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.