Tomasz Sobczyk
04/12/2021, 8:57 PMFrank
Frank
Frank
Tomasz Sobczyk
04/12/2021, 9:16 PMFrank
sst deploy
Frank
sst deploy --stage dev
, sst deploy --stage prod
, or even sst deploy --stage pr23
Tomasz Sobczyk
04/12/2021, 9:17 PMTomasz Sobczyk
04/12/2021, 9:18 PMFrank
Tomasz Sobczyk
04/12/2021, 9:19 PMTomasz Sobczyk
04/12/2021, 9:20 PMFrank
cdk.CfnOutput
with the API endpoint, and they run sst deploy --stage dev --outputs-file outputs.json
, then they grab the endpoint from outputs.json
, sets that in React’s config, then deploys the React app.Tomasz Sobczyk
04/12/2021, 9:32 PMTomasz Sobczyk
04/12/2021, 9:33 PMFrank
Tomasz Sobczyk
04/12/2021, 9:48 PMTomasz Sobczyk
04/12/2021, 9:48 PMFrank
Tomasz Sobczyk
04/12/2021, 9:50 PMFrank
Frank
Tomasz Sobczyk
04/12/2021, 9:51 PMFrank
Tomasz Sobczyk
04/12/2021, 9:53 PMFrank
Frank
Tomasz Sobczyk
04/12/2021, 9:55 PMFrank
Tomasz Sobczyk
04/12/2021, 9:56 PMTomasz Sobczyk
04/12/2021, 9:57 PMFrank
Tomasz Sobczyk
04/12/2021, 9:57 PMFrank
Tomasz Sobczyk
04/12/2021, 9:58 PMTomasz Sobczyk
04/12/2021, 9:58 PMOmi Chowdhury
04/13/2021, 1:58 PMTomasz Sobczyk
04/13/2021, 1:59 PMOmi Chowdhury
04/13/2021, 2:00 PMFrank
Jack Fraser
04/20/2021, 5:09 PMOmi Chowdhury
04/20/2021, 9:47 PMOmi Chowdhury
04/20/2021, 9:49 PMOmi Chowdhury
04/20/2021, 9:54 PM/dev
in the screenshots). For some reason setting an origin path to point to the right APIG stage didn’t work. And I couldn’t get the new cache policies to work (Managed-CacheDisabled seemed ideal).Frank
Tomasz Sobczyk
05/12/2021, 12:19 PMTomasz Michalak
05/12/2021, 12:26 PMTomasz Michalak
05/12/2021, 12:28 PMOmi Chowdhury
05/12/2021, 4:06 PMFrank
Tomasz Sobczyk
05/12/2021, 7:09 PMFrank
main requirement would be to allow dynamically passing the url to the config endpointThis is not supported by CDK out of the box. CDK currently works like this: 1. zips up the React build and uploads to a temporary S3 location 2. deploys API stack’s CF template 3. deploys SPA stack’s CF template (CDK adds a custom resource implemented in Lambda that will download the React build from the temporary S3 locations, unzip it, and syncs it to the static website S3 bucket) So on the first deploy, React app doesn’t know the URL b/c it is built and zipped up (in step 1) before the API is deployed (in step 2). The Solution And our proposed solution is that in you use a placeholder for the API url, ie.
{{ SST_API_URL }}
in your React app. And in Step 3, SST asks the custom resource Lambda function to replace {{ SST_API_URL }}
with the deployed API url in Step 2.
You define the SPA construct like this:
new sst.Spa(this, "SPA", {
indexPage: "index.html",
bucket: "my-spa-bucket",
replaceValues: [
{
file: "*.js",
str: "{{ SST_API_URL }}",
newStr: this.stage === "prod"
? "<http://api-app.customer.com|api-app.customer.com>"
: api.url, // api is the sst.Api construct from another stack
},
],
});
Does this make sense? And are you comfortable with this implementation?Tomasz Sobczyk
05/12/2021, 7:42 PMFrank
Tomasz Sobczyk
05/12/2021, 7:44 PMFrank
Tomasz Michalak
05/13/2021, 9:45 AMTomasz Michalak
05/13/2021, 9:47 AMTomasz Michalak
05/13/2021, 9:52 AMTomasz Michalak
05/20/2021, 9:38 AM➤ YN0000: Preparing your SST app
➤ YN0000: Detected tsconfig.json
➤ YN0000: Transpiling source
➤ YN0000: Running type checker
➤ YN0000: /bin/sh: .../forms-widget/.yarn/cache/typescript-patch-70a7929abf-3be4431759.zip/node_modules/.bin/tsc: Not a directory
during yarn run build
. We use Yarn 2, and it looks that there is some issue with TS & PnP.
We use Yarn workspaces as we split the application to different modules. In the React module, which also uses TS, we have no issue, it ends with:
➤ YN0000: Creating an optimized production build...
➤ YN0000: Compiled successfully.
➤ YN0000:
➤ YN0000: File sizes after gzip:
➤ YN0000:
➤ YN0000: 41.34 KB build/static/js/2.ad6aac36.chunk.js
➤ YN0000: 1.63 KB build/static/js/3.776157f8.chunk.js
➤ YN0000: 1.17 KB build/static/js/runtime-main.115d915c.js
➤ YN0000: 597 B build/static/js/main.2c885fb9.chunk.js
➤ YN0000: 574 B build/static/css/main.9d5b29c0.chunk.css
➤ YN0000:
➤ YN0000: The project was built assuming it is hosted at /.
➤ YN0000: You can control this with the homepage field in your package.json.
➤ YN0000:
➤ YN0000: The build folder is ready to be deployed.
➤ YN0000: You may serve it with a static server:
➤ YN0000:
➤ YN0000: npm install -g serve
➤ YN0000: serve -s build
Any suggestions?Tomasz Michalak
05/20/2021, 9:52 AMnpm install
and npm run build
, the result is correct:
Successfully compiled 1 stack to .build/cdk.out:
dev-json-schema-transformer-my-stack
Frank
Tomasz Michalak
05/20/2021, 6:25 PMFrank
Frank
yarn create serverless-stack my-sst-app --language typescript --use-yarn
resulted in the error:
Internal Error: root-workspace-0b6124@workspace:.: This package doesn't seem to be present in your lockfile; try to make an install to update your resolutions
Yarn v2 expects the project to have a yarn.lock
file, which the SST template app doesn’t have.Frank
Tomasz Michalak
05/21/2021, 5:09 PMFrank
Frank
Frank
Tomasz Sobczyk
05/27/2021, 8:34 AMTomasz Sobczyk
05/27/2021, 8:40 AMFrank
sst start
, use invoke the deployed static website URL, the request gets proxied to your local machine, and fetches the js/html/css resources on your local machine.
Instead, I was thinking to keep SST and React loosely coupled. I explained what I had in mind in the comment. Let me know if that makes sense. I’m hoping to have something ready by next week.Tomasz Sobczyk
05/27/2021, 5:37 PMTomasz Sobczyk
06/08/2021, 8:58 PMTomasz Sobczyk
06/08/2021, 8:58 PMFrank
StaticSite
. We ended up not doing the auto-replacing frontend config for now. The proposed solution in the discussion didn’t feel too clean using it. We will continue to look for a better solution.Frank
StaticSite
is fairly simple with a couple of features:
• the ability to redirect from an alias domain (ie. www.domain.com) to the main domain (ie. domain.com)
• redirect http to https
• specify a bulid command, and the frontend will be built by CDKFrank
cacheControl
settings to different files. For example, don’t cache *.html
and cache *.css *.js
forever. Example here - https://docs.serverless-stack.com/constructs/StaticSite#configure-caching
• StaticSite
now does atomic deploys. Each deployment gets deployed to a new folder in S3 bucket. So you won’t have orphan files from previous deployments.
• You can specify a list of glob patterns, with the search and replace terms for each. Example here - https://docs.serverless-stack.com/constructs/StaticSite#replace-deployed-valuesFrank
{{ API_ENDPOINT }}
in ur React config with the deployed endpoint automatically.Tomasz Michalak
06/23/2021, 12:50 PMTomasz Sobczyk
06/29/2021, 9:24 PMTomasz Sobczyk
06/29/2021, 9:24 PMFrank
Frank
ReactStaticSite
construct. It supports React environment variables. Ie. process.env.REACT_APP_API_URL
in your React app can get automatically replaced with the deployed Api endpoint. You can read more about how it works here - https://docs.serverless-stack.com/constructs/ReactStaticSiteTomasz Michalak
07/15/2021, 7:10 AMFrank