Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optionally load gatsby datastore in lambdas #376

Merged
merged 81 commits into from
May 26, 2022

Conversation

ericapisani
Copy link
Contributor

@ericapisani ericapisani commented May 13, 2022

Summary

Introduce an environment variable that controls whether the Gatsby datastore is bundled in with the lambdas or is uploaded to the CDN and downloaded on first load of the lambdas.

If the environment variable (LOAD_GATSBY_LMDB_DATASTORE_FROM_CDN) is enabled, the plugin now also sends a pre-warm request to the SSR, DSG, and API functions. This is because it can take some time to initially download the datastore, and we want to mitigate the impact on our user's end-users of this operation (they could be waiting an extended period of time and it might look like the page is hanging).

Additional changes

  • Introduce Jest configuration, and the chance, uuid, tmp-package, and tmp-promise packages for unit tests
  • Increased minimum node version to 14.17.0 due to the use of the AbortController in the onSuccess build step
  • Moved execa to a devDependency from peerDependency within the plugin/package.json. This is because we're using it directly within a test, and we also want to ensure that it's on v5 as v6 is entirely in pure ESM which was causing build issues within the project (see release notes)

Test plan

  1. Visit the Deploy Preview - https://ep-optionally-load-gatsby-datastore-in-lambdas.netlify.app/
  2. Click on 'Blog' > 'Second Post'. This is a DSG page so the page should be available and render properly when clicked on.

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

Partially addresses: #320

Doesn't include the following from the linked issue:

  • setting headers config to add basic auth to the db file too
  • modifying the datastore independently to the rest of the build

signal-2022-04-21-20-37-31-546

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

🧪 Once merged, make sure to update the version if needed and that it was
published correctly.

ericapisani added 26 commits May 3, 2022 14:49
Suspect that the function is completing early because this wasn't happening before
thought this would download the file into the directory, but suspecting that the file needs to exist
before the stream writes
aiming to mitigate the effects of the cold start when downloading the
gatsby datastore from the CDN
@ericapisani ericapisani self-assigned this May 13, 2022
@netlify
Copy link

netlify bot commented May 13, 2022

Deploy Preview for netlify-plugin-gatsby-demo ready!

Name Link
🔨 Latest commit 4c26a82
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-gatsby-demo/deploys/628e378ffc389d00087e7a57
😎 Deploy Preview https://deploy-preview-376--netlify-plugin-gatsby-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 13, 2022

Deploy Preview for ep-optionally-load-gatsby-datastore-in-lambdas ready!

Name Link
🔨 Latest commit 4c26a82
🔍 Latest deploy log https://app.netlify.com/sites/ep-optionally-load-gatsby-datastore-in-lambdas/deploys/628e378f315ef70009bb7080
😎 Deploy Preview https://deploy-preview-376--ep-optionally-load-gatsby-datastore-in-lambdas.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

if (
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE === 'true' ||
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE === '1'
) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tl;dr - Opted to hard code this rather than use shouldSkipBundlingDatastore because of import issues that result when the lambda functions are compiled.

Longer version
Initially I had changed this to use the shouldSkipBundlingDatastore method that's in the helpers/config file. While locally this works, when the functions are built, an import error results because helpers/config is not included in the lambda bundle.

We could have included the file in the lambda bundle so that the lambda had a reference to shouldSkipBundlingDatastore, but that didn't make the most sense given that the file mostly contains logic related to the build plugin and the configuration files that should be generated as part of the build process.

We could have also moved the shouldSkipBundlingDatastore and isEnvSet methods (on which shouldSkipBundlingDatastore depends on) into the templates/utils file, but that also didn't make the most sense since these are configuration related details, not related to templating.

Though not perfect, for the time being this seemed like the simplest/most straightforward solution.

plugin/src/helpers/config.ts Show resolved Hide resolved
ensureFileSync(filePath)
}
return downloadFile(downloadUrl, filePath)
}
const dir = 'data'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦🏻‍♂️ oops, sorry

@ericapisani ericapisani requested review from ascorbic and orinokai May 25, 2022 14:41
Copy link
Contributor

@orinokai orinokai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A beast of a PR! All LGTM.

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it! Great work

@grgcnnr
Copy link

grgcnnr commented Aug 24, 2022

For anyone who comes across this in their googling, the environment variable is named GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE in the final solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants