8000 DomainRedirects with env vars · Issue #838 · serverless-nextjs/serverless-next.js · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

DomainRedirects with env vars #838

Closed
1 task
mlecoq opened this issue Dec 7, 2020 · 6 comments
Closed
1 task

DomainRedirects with env vars #838

mlecoq opened this issue Dec 7, 2020 · 6 comments

Comments

@mlecoq
Copy link
mlecoq commented Dec 7, 2020

I have the following configuration

connect:
  component: '@sls-next/serverless-component@1.18.0'
  inputs:
    domain: ${env.DOMAIN}
    domainType: 'both'
    domainRedirects:
      www.${env.DOMAIN}: https://${env.DOMAIN}
    bucketRegion: ${env.APP_BUCKET_REGION}

Describe the bug

I want to use env var to define redirection but it fails (it works with hardcoded values)

Actual behavior

The deployment fails (see logs)

Expected behavior

I should have the same behavior with env vars

Steps to reproduce

deploy a domain redirection with env vars

Screenshots/Code/Logs

Error: domainRedirects: domain.com is invalid. The URL is not in a valid URL format. at Builder.normalizeDomainRedirects (/root/.serverless/components/registry/npm/@sls-next/serverless-component@1.18.0/node_modules/@sls-next/lambda-at-edge/dist/build.js:435:23)

Versions

  • OS/Environment: Linux
  • @sls-next/serverless-component version: 1.8.0
  • Next.js version: 10

Additional context

Checklist

  • [ X ] You have reviewed the README and FAQs, which answers several common questions.
  • Please first try using the most recent latest or alpha @sls-next/serverless-component release version, which may have already fixed your issue or implemented the feature you are trying to use. Note that the old serverless-next.js component and the serverless-next.js plugin are deprecated and no longer maintained.
@dphang
Copy link
Collaborator
dphang commented Dec 17, 2020

The code tries to create a URL from the given value, if there is an exception then it's not a valid URL:

Is it because you are missing the protocol e.g https://. Make sure you are prepending the value with https://. Though from your configuration it has it, so not sure why it is failing...

@krikork
Copy link
krikork commented Dec 28, 2020

I have the same issue on @1.19.0-alpha.21

Hardcoded value like this works:

    domainRedirects:
      sandbox.mydomain.io: https://www.sandbox.mydomain.io 

This fails:

    domainRedirects:
      "${env.DOMAIN}": https://www.${env.DOMAIN}

(I've tried without the quotations too)

Here's the log output:

Error: domainRedirects: sandbox.mydomain.io is invalid. The URL is not in a valid URL format.
                                 at Builder.normalizeDomainRedirects (/root/.serverless/components/registry/npm/@sls-next/serverless-component@1.19.0-alpha.21/node_modules/@sls-next/lambda-at-edge/dist/build.js:514:23)
                                 at Builder.<anonymous> (/root/.serverless/components/registry/npm/@sls-next/serverless-component@1.19.0-alpha.21/node_modules/@sls-next/lambda-at-edge/dist/build.js:238:18)
                                 at Generator.next (<anonymous>)
                                 at fulfilled (/root/.serverless/components/registry/npm/@sls-next/serverless-component@1.19.0-alpha.21/node_modules/@sls-next/lambda-at-edge/dist/build.js:5:58)

@dphang
Copy link
Collaborator
dphang commented Dec 28, 2020

The error is the same, it is because https://www. is being removed when you add that way, maybe it is not the proper way to concatenate in serverless.yml configuration? I am not sure why https://www. is being removed. Could you please specify the full URL in the environment variable. For example, create a new environment variable, called env.DOMAIN_REDIRECT and set it to https://www.sandbox.mydomain.io

@dphang
Copy link
Collaborator
dphang commented Jan 5, 2021

Update: it seems it might be an issue with Serverless Framework? I did see a few very similate issues recently where if you have ${env.VARIABLE}-blah it seems to ignore the concatenation and only take${env.VARIABLE}. Perhaps it might be fixed in the newest version but since we are on the Components Beta it might still be an issue there.

See for a workaround: #530 (comment)

It also doesn't work for me, my recommendation is don't use concatenated env variable or workaround above.

I'll close this issue for now since there appears to be a workaround and update the docs to mention this.

@dphang dphang closed this as completed Jan 5, 2021
@mlecoq
Copy link
Author
mlecoq commented Jan 6, 2021

Thanks @dphang

@mtwalsh
Copy link
mtwalsh commented May 5, 2022

For anyone else running into this issue, I've tried the following:

domain: ${env.DOMAIN}
subdomain: ${env.SUBDOMAIN}

myNextApplication:
  ...
  inputs:
    ...
    domain:
      - ${env.SUBDOMAIN}
      - ${env.DOMAIN}
    domainType: "both"
    domainRedirects:
      "${domain}": https://${subdomain}.${domain}

And also without the quotes, so ${domain}: https://${subdomain}.${domain}.

So despite using the workaround for concatenating env vars: #530 (comment), still no joy.

When I export the Lambda function I can see in the manifest.json both options (with/without quotes) result in:

"domainRedirects":{"${domain}":"https://www.example.com"}

So I assume that's the issue right there, it's not able to expand an environment variable when it's used as the YAML key? But the value does work just fine.

I've just switched to hardcoding, which works and is not really the end of the world either!

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

No branches or pull requests

4 participants
0