-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(netlify): support included/excluded files
- Loading branch information
Showing
16 changed files
with
228 additions
and
521 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
'@astrojs/netlify': minor | ||
--- | ||
|
||
Adds "includedFiles" and "excludedFiles" options. These allow extra files to be deployed in the SSR function bundle. | ||
|
||
When an Astro site using `server` or `hybrid` rendering is deployed to Netlify, the generated functions trace the server dependencies and include any that may be needed in SSR. However sometimes you may want to include extra files that are not detected as dependencies, such as files that are loaded using `fs` functions. Also, you may sometimes want to specifically exclude dependencies that are bundled automatically. For example, you may have a Node module that includes a large binary. The `"includedFiles"` and `"excludedFiles"` options allow you to do these. Both options support glob patterns, so you can include/exclude multiple files at once. | ||
|
||
The paths are relative to the site root. If you are loading them using filesystem funcitons, make sure you resolve paths relative to the site root, and not relative to the cxource file. At runtime the compiled file will be in a different location, so paths that are relative to the file will not work. You should instead resolve the path using `path.resolve()` or `process.cwd()`, which will give you the site root. | ||
|
||
```js | ||
import netlify from '@astrojs/netlify'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
output: 'server', | ||
adapter: netlify({ | ||
includedFiles: ['files/**/*.csv', 'files/include-this.txt'], | ||
excludedFiles: ['files/subdirectory/not-this.csv'], | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/netlify/test/functions/fixtures/includes/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import netlify from '@astrojs/netlify'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
output: 'server', | ||
adapter: netlify({ | ||
includedFiles: ['files/**/*.csv', 'files/include-this.txt'], | ||
excludedFiles: ['files/subdirectory/not-this.csv'], | ||
}), | ||
site: `http://example.com`, | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/netlify/test/functions/fixtures/includes/files/also-this.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1,2,3 |
1 change: 1 addition & 0 deletions
1
packages/netlify/test/functions/fixtures/includes/files/include-this.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello |
1 change: 1 addition & 0 deletions
1
packages/netlify/test/functions/fixtures/includes/files/subdirectory/and-this.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1,2,3 |
1 change: 1 addition & 0 deletions
1
packages/netlify/test/functions/fixtures/includes/files/subdirectory/not-this.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1,2,3 |
1 change: 1 addition & 0 deletions
1
packages/netlify/test/functions/fixtures/includes/files/subdirectory/or-this.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello |
13 changes: 13 additions & 0 deletions
13
packages/netlify/test/functions/fixtures/includes/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@test/netlify-includes", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"@astrojs/netlify": "workspace:", | ||
"astro": "^4.11.5" | ||
}, | ||
"scripts": { | ||
"build": "astro build", | ||
"dev": "astro dev" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="astro/client" /> |
7 changes: 7 additions & 0 deletions
7
packages/netlify/test/functions/fixtures/includes/src/pages/404.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
export const prerender = false | ||
const header = Astro.request.headers.get("x-test") | ||
--- | ||
|
||
<p>This is my custom 404 page</p> | ||
<p>x-test: {header}</p> |
12 changes: 12 additions & 0 deletions
12
packages/netlify/test/functions/fixtures/includes/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
import { promises as fs } from 'fs'; | ||
const cwd = process.cwd(); | ||
const file = await fs.readFile('files/include-this.txt', 'utf-8'); | ||
--- | ||
<html> | ||
<head><title>Testing</title></head> | ||
<body> | ||
<h1>{file}</h1> | ||
<p>{cwd}</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as assert from 'node:assert/strict'; | ||
import { existsSync } from 'node:fs'; | ||
import { before, describe, it } from 'node:test'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { loadFixture } from '@astrojs/test-utils'; | ||
import * as cheerio from 'cheerio'; | ||
|
||
describe( | ||
'Included files', | ||
() => { | ||
let fixture; | ||
const root = new URL('./fixtures/includes/', import.meta.url); | ||
const expectedCwd = new URL( | ||
'.netlify/v1/functions/ssr/packages/netlify/test/functions/fixtures/includes/', | ||
root | ||
); | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ root }); | ||
await fixture.build(); | ||
}); | ||
|
||
it('Includes files', async () => { | ||
const filesRoot = new URL('./files/', expectedCwd); | ||
const expectedFiles = ['include-this.txt', 'also-this.csv', 'subdirectory/and-this.csv']; | ||
|
||
for (const file of expectedFiles) { | ||
assert.ok(existsSync(new URL(file, filesRoot)), `Expected file ${file} to exist`); | ||
} | ||
|
||
const notExpectedFiles = ['subdirectory/not-this.csv', 'subdirectory/or-this.txt']; | ||
|
||
for (const file of notExpectedFiles) { | ||
assert.ok(!existsSync(new URL(file, filesRoot)), `Expected file ${file} to not exist`); | ||
} | ||
}); | ||
|
||
it('Can load included files correctly', async () => { | ||
const entryURL = new URL( | ||
'./fixtures/includes/.netlify/v1/functions/ssr/ssr.mjs', | ||
import.meta.url | ||
); | ||
const { default: handler } = await import(entryURL); | ||
const resp = await handler(new Request('http://example.com/'), {}); | ||
const html = await resp.text(); | ||
const $ = cheerio.load(html); | ||
assert.equal($('h1').text(), 'hello'); | ||
assert.equal($('p').text(), fileURLToPath(expectedCwd).slice(0, -1)); | ||
}); | ||
}, | ||
{ | ||
timeout: 120000, | ||
} | ||
); |
Oops, something went wrong.