Skip to content

Allow setting additional MIME-Types / file extensions that will be returned in single-page-app #429

@RianFuro

Description

@RianFuro

Start from the Use-case

The single-page-app construct sets a CloudFront function that tests for file extensions that will pass, redirecting all other requested files to the SPA Root:
https://github.com/getlift/lift/blob/fc97b0be0a340a85c86580cdd4adb5ac002a7a92/src/constructs/aws/SinglePageApp.ts#L48C9-L60C4

I'd like to serve .babylon files as well. However, without any ability to customize this whitelist, I'd have to go quite the way around the construct just to allow this.
Would it be in scope for lift to add a configuration option to extend the list of permitted extensions?

Example Config

constructs:
  spa:
    type: single-page-app
    allowedFiletypes:
      - babylon

Implementation Idea

Aside from adding a configuration option, the change should be quite straight forward, as we'd just need to modify the cf-function slightly:

+        let extraFiletypes = configuration.allowedFiletypes.join('|')
+        if (extraFiletypes) extraFiletypes = '|' + extraFiletypes
-        const code = `var REDIRECT_REGEX = /^[^.]+$|\\.(?!(css|gif|ico|jpg|jpeg|js|png|txt|svg|woff|woff2|ttf|map|json|webp|xml|pdf|webmanifest|avif|wasm)$)([^.]+$)/;
+        const code = `var REDIRECT_REGEX = /^[^.]+$|\\.(?!(css|gif|ico|jpg|jpeg|js|png|txt|svg|woff|woff2|ttf|map|json|webp|xml|pdf|webmanifest|avif|wasm${extraFiletypes})$)([^.]+$)/;


function handler(event) {
    var uri = event.request.uri;
    var request = event.request;
    var isUriToRedirect = REDIRECT_REGEX.test(uri);


    if (isUriToRedirect) {
        request.uri = "/index.html";
    }${additionalCode}


    return event.request;
}`;

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions