Daily Rebuild Netlify Function
Relevant source files
The following files were used as context for generating this wiki page:
The Daily Rebuild Netlify Function triggers a scheduled deployment of the website to update time-sensitive content. Its primary purpose is to refresh the "Verse of the Day" displayed on the site by forcing a static site regeneration. Sources: README.md:10, netlify/functions/daily-rebuild/index.ts:16-18
This function operates within the Netlify Functions environment and uses a cron-based schedule. It communicates with Netlify's build system via a secure build hook URL configured in the project's environment variables. Sources: netlify/functions/daily-rebuild/index.ts:7-9, netlify.toml:1-2
Architecture and Execution Flow
The function exports a config object that defines its execution schedule and a handler function that performs the rebuild trigger. It validates the presence of the required build hook before attempting to contact the Netlify API. Sources: netlify/functions/daily-rebuild/index.ts:7-35
Execution Logic
The logic follows a linear path of validation and execution:
- Schedule Check: The Netlify runtime triggers the function based on the cron expression
0 10 * * *. - Environment Validation: The function checks for the
NETLIFY_BUILD_HOOK_URLenvironment variable. - HTTP Request: If the URL exists, the function sends an HTTP POST request to that endpoint.
- Error Handling: If the request fails (non-2xx status), the function throws an error to signal a failed execution.
The diagram shows the logic flow from the scheduled trigger to the final response or error state. Sources: netlify/functions/daily-rebuild/index.ts:16-31
Configuration
The function's behavior and scheduling are controlled by the config export and environment variables.
| Property | Type | Value / Source | Description |
|---|---|---|---|
schedule | String | 0 10 * * * | Cron expression for 10:00 UTC daily. |
NETLIFY_BUILD_HOOK_URL | String | Environment Variable | The destination URL for the build trigger. |
directory | String | netlify/functions | The location of functions in the project. |
Sources: netlify/functions/daily-rebuild/index.ts:8, netlify.toml:2, netlify/functions/daily-rebuild/index.test.ts:20-22
Key Implementation Details
The function uses standard Fetch API methods to interact with the Netlify Build Hook.
// netlify/functions/daily-rebuild/index.ts:16-31
export async function triggerDailyRebuild({
env = process.env,
fetch: fetchFn = globalThis.fetch,
log = console,
}: DailyRebuildOptions = {}) {
const buildHookUrl = env.NETLIFY_BUILD_HOOK_URL
if (!buildHookUrl) {
log.info('NETLIFY_BUILD_HOOK_URL is unset; skipping scheduled rebuild')
return new Response('skipped', { status: 200 })
}
const response = await fetchFn(buildHookUrl, { method: 'POST' })
if (!response.ok) {
throw new Error(`Build hook failed with ${response.status}`)
}
return response
}
Sources: netlify/functions/daily-rebuild/index.ts:16-31
Testing and Validation
The implementation is verified using node:test and mocks to ensure correct behavior when environment variables are missing or API calls fail. Sources: netlify/functions/daily-rebuild/index.test.ts:1-73
- Mocking: Tests mock the
fetchfunction andconsole.logto verify interactions. - Assertions: The suite asserts that missing URLs result in a
200 skippedresponse rather than an error. - Failure Cases: The suite confirms that an error is thrown if the build hook returns a
500status code.
Sources: netlify/functions/daily-rebuild/index.test.ts:33-73
Integration with Site Content
The rebuild is specifically timed to support the Verse of the Day feature. The fetchTodayVerse utility determines which verse to display based on the dayOfYear. Because the site is built with Astro as a static site, a daily rebuild is required to update the HTML with the current day's verse retrieved from the YouVersion API. Sources: src/features/verse/verse.utils.ts:25-30, README.md:10
Summary: The function ensures the static site remains current by triggering a new build every 24 hours, primarily serving the dynamic needs of the Bible verse integration.