From 56be2153c74b2c44d87ce0e5e404a832206c9f75 Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 30 Oct 2022 20:17:18 +0800 Subject: [PATCH 1/3] fix: make index and error documents configurable --- gatsby-plugin-s3/src/bin.ts | 4 ++-- gatsby-plugin-s3/src/constants.ts | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/gatsby-plugin-s3/src/bin.ts b/gatsby-plugin-s3/src/bin.ts index 3eb6d2c..4ce0780 100644 --- a/gatsby-plugin-s3/src/bin.ts +++ b/gatsby-plugin-s3/src/bin.ts @@ -209,10 +209,10 @@ export const deploy = async ({ yes, bucket, userAgent }: DeployArguments = {}) = Bucket: config.bucketName, WebsiteConfiguration: { IndexDocument: { - Suffix: 'index.html', + Suffix: config.indexDocumentSuffix ?? 'index.html', }, ErrorDocument: { - Key: '404.html', + Key: config.errorDocumentKey ?? '404.html', }, }, }; diff --git a/gatsby-plugin-s3/src/constants.ts b/gatsby-plugin-s3/src/constants.ts index fb9acb5..f1c8948 100644 --- a/gatsby-plugin-s3/src/constants.ts +++ b/gatsby-plugin-s3/src/constants.ts @@ -92,6 +92,13 @@ export interface S3PluginOptions extends PluginOptions { // the static website hosting functionality. enableS3StaticWebsiteHosting?: boolean; + // Index document suffix. See https://docs.aws.amazon.com/AmazonS3/latest/API/API_IndexDocument.html. + indexDocumentSuffix?: string; + + // Error document object key name to use when a 4XX class error occurs. + // See https://docs.aws.amazon.com/AmazonS3/latest/API/API_ErrorDocument.html. + errorDocumentKey?: string; + // Max number of files to upload in parallel. parallelLimit?: number; From ca3ec1360cc73b4852fe335e2235cb104805be33 Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 30 Oct 2022 20:17:22 +0800 Subject: [PATCH 2/3] fix: make index and error documents configurable --- README.md | 2 ++ docs/.awesome/content/index.md | 2 ++ examples/with-redirects/gatsby-config.js | 2 ++ .../with-redirects/src/pages/another404.js | 11 ++++++++ gatsby-plugin-s3-e2e-tests/src/e2e.test.ts | 25 +++++++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 examples/with-redirects/src/pages/another404.js diff --git a/README.md b/README.md index 938f3e7..f7a6e73 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,8 @@ As mentioned above, the `bucketName` is required, but there's much more to confi | `retainObjectsPatterns` | `string array \| undefined` | `[]` | An array of file globs that should not be removed from the S3 bucket if the file does not exist locally, this does nothing unless `removeNonexistentObjects` is true. [See here for glob format](https://github.com/isaacs/node-glob#glob-primer) | | `customAwsEndpointHostname` | `string \| undefined` | `amazonaws.com` | Custom AWS S3 endpoint. [See our docs for all available options](https://gatsby-plugin-s3.jari.io/recipes/custom-endpoint) | | `enableS3StaticWebsiteHosting` | `boolean \| undefined` | `true` | Disables modifications to the S3 Static Website Hosting configuration. Without S3 Static Website Hosting some features (index.html rewriting, trailing slash redirects, and serverside redirects), will not function. Not recommended, but could be useful for preventing Cloud formation Stack Drift or suppressing Terraform noise if you don't need, the static website hosting functionality. | +| `indexDocumentSuffix` | `string \| undefined` | `index.html` | Index document suffix. See https://docs.aws.amazon.com/AmazonS3/latest/API/API_IndexDocument.html. | +| `errorDocumentKey` | `string \| undefined` | `404.html` | Error document object key name to use when a 4XX class error occurs. See https://docs.aws.amazon.com/AmazonS3/latest/API/API_ErrorDocument.html. | | `parallelLimit` | `number \| undefined` | `20` | Max number of files to upload in parallel. | | `maxRetries` | `number \| undefined` | `undefined` | The maximum amount of retries to perform for a service request. | | `connectTimeout` | `number \| undefined` | `undefined` | The maximum time in milliseconds that the connection phase of the request should be allowed to take. This only limits the connection phase and has no impact once the socket has established a connection. | diff --git a/docs/.awesome/content/index.md b/docs/.awesome/content/index.md index 1ff5474..adee866 100644 --- a/docs/.awesome/content/index.md +++ b/docs/.awesome/content/index.md @@ -93,6 +93,8 @@ Default configuration is as follows: retainObjectsPatterns: [], customAwsEndpointHostname: undefined, enableS3StaticWebsiteHosting: true, + indexDocumentSuffix: 'index.html', + errorDocumentKey: '404.html', parallelLimit: 20, maxRetries: undefined; connectTimeout: undefined; diff --git a/examples/with-redirects/gatsby-config.js b/examples/with-redirects/gatsby-config.js index ddc82ed..c338629 100644 --- a/examples/with-redirects/gatsby-config.js +++ b/examples/with-redirects/gatsby-config.js @@ -22,6 +22,8 @@ module.exports = { : {}), removeNonexistentObjects: true, retainObjectsPatterns: ['**/*.retain.js', '**/retain-folder/*'], + indexDocumentSuffix: process.env.GATSBY_S3_INDEX_DOCUMENT_SUFFIX || 'index.html', + errorDocumentKey: process.env.GATSBY_S3_ERROR_DOCUMENT_KEY || '404.html', }, }, { diff --git a/examples/with-redirects/src/pages/another404.js b/examples/with-redirects/src/pages/another404.js new file mode 100644 index 0000000..14b3915 --- /dev/null +++ b/examples/with-redirects/src/pages/another404.js @@ -0,0 +1,11 @@ +import React from 'react'; +import Layout from '../components/layout'; + +const NotFoundPage = () => ( + +

ANOTHER NOT FOUND

+

You just hit a route that doesn't exist... the sadness.

+
+); + +export default NotFoundPage; diff --git a/gatsby-plugin-s3-e2e-tests/src/e2e.test.ts b/gatsby-plugin-s3-e2e-tests/src/e2e.test.ts index d3de8f9..8fcfe63 100644 --- a/gatsby-plugin-s3-e2e-tests/src/e2e.test.ts +++ b/gatsby-plugin-s3-e2e-tests/src/e2e.test.ts @@ -374,3 +374,28 @@ describe('with pathPrefix', () => { }); }); }); + +describe('custom error document', () => { + beforeAll(async () => { + await buildSite('gatsby-plugin-s3-example-with-redirects', { + GATSBY_S3_TARGET_BUCKET: bucketName, + GATSBY_S3_ERROR_DOCUMENT_KEY: 'another404.html', + }); + await deploySite('gatsby-plugin-s3-example-with-redirects', [ + Permission.PutObject, + Permission.PutObjectAcl, + Permission.CreateBucket, + Permission.PutBucketAcl, + Permission.PutBucketWebsite, + ]); + }); + + test(`uses custom error document`, async () => { + const path = `/non-existing`; + const fullPath = `${testingEndpoint}${path}`; + const response = await fetch(fullPath); + expect(response.status, `This path should not exist ${fullPath}`).toBe(404); + const html = await response.text(); + expect(html.indexOf('ANOTHER'), `Wrong error document used`).toBeGreaterThan(0); + }); +}); \ No newline at end of file From cb4c5cf1979cd630a80bd78a77f570a8c0e9f20f Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 30 Oct 2022 20:21:25 +0800 Subject: [PATCH 3/3] fix: make index and error documents configurable --- gatsby-plugin-s3-e2e-tests/src/e2e.test.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/gatsby-plugin-s3-e2e-tests/src/e2e.test.ts b/gatsby-plugin-s3-e2e-tests/src/e2e.test.ts index 8fcfe63..58d711c 100644 --- a/gatsby-plugin-s3-e2e-tests/src/e2e.test.ts +++ b/gatsby-plugin-s3-e2e-tests/src/e2e.test.ts @@ -391,10 +391,9 @@ describe('custom error document', () => { }); test(`uses custom error document`, async () => { - const path = `/non-existing`; - const fullPath = `${testingEndpoint}${path}`; - const response = await fetch(fullPath); - expect(response.status, `This path should not exist ${fullPath}`).toBe(404); + const path = `${testingEndpoint}/random`; + const response = await fetch(path); + expect(response.status, `This path should not exist ${path}`).toBe(404); const html = await response.text(); expect(html.indexOf('ANOTHER'), `Wrong error document used`).toBeGreaterThan(0); });