Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: put server assets on CDN #9278

Merged
merged 14 commits into from
Mar 5, 2024
Merged

chore: put server assets on CDN #9278

merged 14 commits into from
Mar 5, 2024

Conversation

mattkrick
Copy link
Member

@mattkrick mattkrick commented Dec 6, 2023

Description

Publicly host server assets so they can be used by downstream services like email, mattermost, etc.

fix #9111
fix #8553

Demo

Testing scenarios

in dev, with no CDN

in prod, with no CDN (after running preDeploy)

  • http://localhost:3000/jira-attachments/foo.png results in a image not found .png
  • notifications published to mattermost include an absolute url starting with env.HOST
  • images in manifest.json are available
  • emails using anonymous avatar work correctly

in prod, with CDN (after running preDeploy)

  • http://localhost:3000/jira-attachments/foo.png results in a image not found .png
  • notifications published to mattermost include an absolute url starting with CDN_BASE_URL
  • images in manifest.json are available
  • templates upload to the existing location

Signed-off-by: Matt Krick <matt.krick@gmail.com>
@github-actions github-actions bot added the size/m label Dec 6, 2023
Signed-off-by: Matt Krick <matt.krick@gmail.com>
Signed-off-by: Matt Krick <matt.krick@gmail.com>
Signed-off-by: Matt Krick <matt.krick@gmail.com>
Signed-off-by: Matt Krick <matt.krick@gmail.com>
Signed-off-by: Matt Krick <matt.krick@gmail.com>
@@ -0,0 +1,16 @@
import appOrigin from './appOrigin'

declare let __webpack_public_path__: string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't set the public path at buildtime because different PPMIs will host their assets in different places.
since the public path is defined during predeploy, we must set it at runtime

path.join(__dirname, jiraPlaceholder.slice(__webpack_public_path__.length))
)
try {
const res = await fetch(jiraPlaceholder)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all other assets need a public URL except this one, so instead of special casing this one I just turned it into a fetch vs. a file system read.

[path.join(PROJECT_ROOT, 'static')]: !__PRODUCTION__,
[path.join(PROJECT_ROOT, 'dev', 'dll')]: !__PRODUCTION__
// publish server assets at /static
[path.join(PROJECT_ROOT, 'dist')]: __PRODUCTION__,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path is left intact, e.g. dist/images/imageNotFound.png will be published at /static/images/imageNotFound.png

@@ -82,7 +78,7 @@ const rewriteIndexHTML = () => {
const keys = `<script>window.__ACTION__=${JSON.stringify(clientKeys)}</script>`
const rawHTML = skeleton
.replace('<head>', `<head>${noindex}${keys}`)
.replaceAll('__PUBLIC_PATH__', getCDNURL())
.replaceAll('__PUBLIC_PATH__', __webpack_public_path__.replace(/\/$/, ''))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove trailing slash

@@ -2,6 +2,11 @@ import fs from 'fs'
import getFileStoreManager from 'parabol-server/fileStorage/getFileStoreManager'
import path from 'path'
import getProjectRoot from '../webpack/utils/getProjectRoot'
;(require as any).context(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the DB needs these files, we require them here so they get added to the server bundle, e.g. dist/images/templates

@@ -25,46 +30,78 @@ const pushClientAssetsToCDN = async () => {
console.log(`⛅️ Uploaded ${dirEnts.length} client assets to CDN`)
}

const pushTemplatesToCDN = async () => {
const pushServerAssetsToCDN = async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we were just pushing client assets, now we push server assets, too.
This is useful for things like:

  • anonymousAvatar getting referenced in emails
  • our logo's URL getting published in mattermost messages

@@ -25,8 +26,8 @@ module.exports = {
__dirname: false
},
entry: {
web: [DOTENV, path.join(SERVER_ROOT, 'server.ts')],
gqlExecutor: [DOTENV, path.join(GQL_ROOT, 'gqlExecutor.ts')]
web: [DOTENV, INIT_PUBLIC_PATH, path.join(SERVER_ROOT, 'server.ts')],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the public path has to get set before imports use it, so set it here because that way the import order of server.ts doesn't matter

Signed-off-by: Matt Krick <matt.krick@gmail.com>
Signed-off-by: Matt Krick <matt.krick@gmail.com>
@mattkrick mattkrick removed the request for review from BartoszJarocki December 13, 2023 22:28
@mattkrick mattkrick marked this pull request as draft December 13, 2023 22:28
Signed-off-by: Matt Krick <matt.krick@gmail.com>
Signed-off-by: Matt Krick <matt.krick@gmail.com>
@@ -129,5 +130,5 @@ jobs:
uses: actions/upload-artifact@v2
with:
name: test-results
path: test-results/
path: packages/integration-tests/test-results/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has been wrong since forever, i just didn't ever need it until today 😞

@mattkrick mattkrick marked this pull request as ready for review December 14, 2023 00:16
@mattkrick mattkrick requested review from igorlesnenko and removed request for BartoszJarocki January 24, 2024 20:19
@mattkrick
Copy link
Member Author

@igor would you mind giving this a review?

Copy link
Contributor

@igorlesnenko igorlesnenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do: yarn build, yarn predeploy, yarn start
And images of meetings are not displayed. Am I missing something?
image

@@ -47,8 +45,6 @@ const writeManifest = () => {
const manifestPath = path.join(clientDir, 'manifest.json')
fs.writeFileSync(manifestPath, JSON.stringify(manifest))
// move the referenced icons into the client build
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not moving already

Signed-off-by: Matt Krick <matt.krick@gmail.com>
Signed-off-by: Matt Krick <matt.krick@gmail.com>
Signed-off-by: Matt Krick <matt.krick@gmail.com>
@mattkrick mattkrick merged commit 06c1f7e into master Mar 5, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Accessing jira-attachments will result in a 5xx parabol icon in mattermost should be absolute
3 participants