From 395bbc0d846401efcffb9c3b8ff6bd0595cc744b Mon Sep 17 00:00:00 2001 From: James Zetlen Date: Wed, 2 Dec 2020 14:32:15 -0600 Subject: [PATCH] [PWA-1128] fix(buildpack): Stop apicache from overriding good cache headers. (#2870) * fix(buildpack): Stop apicache from overriding good cache headers. Fixes PWA-1128. * fix(venia-ui): add cache headers to upward.yml staticFromRoot resolver * fix: add simple max-age behavior to upward DirectoryResolver Co-authored-by: Devagouda <40405790+dpatil-magento@users.noreply.github.com> --- .../__tests__/addImgOptMiddleware.spec.js | 2 +- .../lib/Utilities/addImgOptMiddleware.js | 7 ++- packages/pwa-buildpack/package.json | 2 +- .../lib/resolvers/DirectoryResolver.js | 17 +++--- packages/venia-ui/upward.yml | 8 +++ yarn.lock | 59 +++++++++++++------ 6 files changed, 68 insertions(+), 27 deletions(-) diff --git a/packages/pwa-buildpack/lib/Utilities/__tests__/addImgOptMiddleware.spec.js b/packages/pwa-buildpack/lib/Utilities/__tests__/addImgOptMiddleware.spec.js index 7efec62460..b5e817a63a 100644 --- a/packages/pwa-buildpack/lib/Utilities/__tests__/addImgOptMiddleware.spec.js +++ b/packages/pwa-buildpack/lib/Utilities/__tests__/addImgOptMiddleware.spec.js @@ -43,7 +43,7 @@ test('attaches middleware to app', () => { expect.objectContaining({ force: false }) ); - expect(app.use).toHaveBeenCalledWith(mockCacheMiddleware, filterMiddleware); + expect(app.use).toHaveBeenCalled(); expect(filterMiddleware).toBeTruthy(); }); diff --git a/packages/pwa-buildpack/lib/Utilities/addImgOptMiddleware.js b/packages/pwa-buildpack/lib/Utilities/addImgOptMiddleware.js index 80bb20e242..7fd755d3f2 100644 --- a/packages/pwa-buildpack/lib/Utilities/addImgOptMiddleware.js +++ b/packages/pwa-buildpack/lib/Utilities/addImgOptMiddleware.js @@ -1,5 +1,6 @@ const debug = require('../util/debug').makeFileLogger(__filename); let cache; +/** @type {import("hastily")} */ let hastily; let missingDeps = ''; const markDepInvalid = (dep, e) => { @@ -56,7 +57,11 @@ If possible, install additional tools to build NodeJS native dependencies: https://github.com/nodejs/node-gyp#installation` ); } else { - app.use(cacheMiddleware, imageopto); + app.use( + hastily.HASTILY_STREAMABLE_PATH_REGEXP, + cacheMiddleware, + imageopto + ); } } diff --git a/packages/pwa-buildpack/package.json b/packages/pwa-buildpack/package.json index 172adbc15b..1b5cb30c43 100644 --- a/packages/pwa-buildpack/package.json +++ b/packages/pwa-buildpack/package.json @@ -45,7 +45,7 @@ "fs-extra": "~7.0.1", "gitignore-to-glob": "~0.3.0", "graphql-playground-middleware-express": "~1.7.18", - "hastily": "~0.4.7", + "hastily": "~0.4.9", "js-yaml": "~3.13.1", "jsdom": "~16.2.2", "klaw": "~3.0.0", diff --git a/packages/upward-js/lib/resolvers/DirectoryResolver.js b/packages/upward-js/lib/resolvers/DirectoryResolver.js index 03138774de..12494a1d51 100644 --- a/packages/upward-js/lib/resolvers/DirectoryResolver.js +++ b/packages/upward-js/lib/resolvers/DirectoryResolver.js @@ -30,17 +30,20 @@ class DirectoryResolver extends AbstractResolver { let server = DirectoryResolver.servers.get(directory); if (!server) { + const staticOpts = { + fallthrough: false, + index: false, + maxAge: process.env.NODE_ENV === 'production' ? 604800000 : 0 + }; debug( - `creating new server for ${directory} relative to ${ - this.visitor.upwardPath - }` + `creating new server for directory "%s" relative to "%s" with options %o`, + directory, + this.visitor.upwardPath, + staticOpts ); server = serveStatic( path.resolve(path.dirname(this.visitor.upwardPath), directory), - { - fallthrough: false, - index: false - } + staticOpts ); DirectoryResolver.servers.set(directory, server); } diff --git a/packages/venia-ui/upward.yml b/packages/venia-ui/upward.yml index a0d009723a..42ff06420b 100644 --- a/packages/venia-ui/upward.yml +++ b/packages/venia-ui/upward.yml @@ -31,6 +31,14 @@ staticFromRoot: resolver: inline inline: content-type: contentTypeFromExtension + cache-control: + when: + - matches: env.NODE_ENV + pattern: 'production' + use: + inline: public, max-age=604800 + default: + inline: no-cache, no-store, must-revalidate body: resolver: file parse: diff --git a/yarn.lock b/yarn.lock index b79daa1515..c42cc9781d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7500,6 +7500,11 @@ fast-redact@^2.0.0: resolved "https://registry.yarnpkg.com/fast-redact/-/fast-redact-2.0.0.tgz#17bb8f5e1f56ecf4a38c8455985e5eab4c478431" integrity sha512-zxpkULI9W9MNTK2sJ3BpPQrTEXFNESd2X6O1tXMFpK/XM0G5c5Rll2EVYZH2TqI3xRGK/VaJ+eEOt7pnENJpeA== +fast-redact@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/fast-redact/-/fast-redact-3.0.0.tgz#ac2f9e36c9f4976f5db9fb18c6ffbaf308cf316d" + integrity sha512-a/S/Hp6aoIjx7EmugtzLqXmcNsyFszqbt6qQ99BdG61QjBZF6shNis0BYR6TsZOQ1twYc0FN2Xdhwwbv6+KD0w== + fast-safe-stringify@^2.0.7: version "2.0.7" resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz#124aa885899261f68aedb42a7c080de9da608743" @@ -8675,19 +8680,19 @@ hast-util-parse-selector@^2.0.0: resolved "https://registry.yarnpkg.com/hast-util-parse-selector/-/hast-util-parse-selector-2.2.3.tgz#57edd449103900c7f63fd9e6f694ffd7e4634719" integrity sha512-nxbeqjQNxsvo/uYYAw9kij6td05YVUlf1qti09rVfbWSLT5H6wo3c+USIwX6nzXWk5kFZzXnEqO82856r0aM2Q== -hastily@~0.4.7: - version "0.4.7" - resolved "https://registry.yarnpkg.com/hastily/-/hastily-0.4.7.tgz#3bb105586019708cd6c464d7c0fd710aae6b9bc3" - integrity sha512-sHih6UGApmVJ6jGdqDLjfmwkVo3bkw6/NK/3tyM/OK8rRm+mucwv+Q8Fdl2bWQT5gowoVDh0FM3wETWaTbfIFQ== +hastily@~0.4.9: + version "0.4.9" + resolved "https://registry.yarnpkg.com/hastily/-/hastily-0.4.9.tgz#e67af5b661a4d42990363bfba09ee29f3df670bf" + integrity sha512-14R92v0wjsXM4NM89xgsYELu1DmgVhwS4JT4giPtvvBBklj97uDnVNK2JcLL3albcNfwiecNEJhNykESsuc0qw== dependencies: accepts "^1.3.7" debug "^4.1.1" mime "^2.4.6" mime-types "^2.1.27" on-headers "^1.0.2" - pino "^6.3.2" + pino "^6.5.1" pino-http "^5.2.0" - pino-pretty "^4.0.0" + pino-pretty "^4.1.0" vary "^1.1.2" hastscript@^5.0.0: @@ -12681,14 +12686,14 @@ pino-http@^5.2.0: pino "^6.0.0" pino-std-serializers "^2.4.0" -pino-pretty@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/pino-pretty/-/pino-pretty-4.0.0.tgz#afbff81f946342b9d6eabc434942fe490e02faa9" - integrity sha512-YLy/n3dMXYWOodSm530gelkSAJGmEp29L9pqiycInlIae5FEJPWAkMRO3JFMbIFtjD2Ve4SH2aBcz2aRreGpBQ== +pino-pretty@^4.1.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/pino-pretty/-/pino-pretty-4.3.0.tgz#18695606fd4f1e21cd1585d18999cd84d429e1d8" + integrity sha512-uEc9SUCCGVEs0goZvyznKXBHtI1PNjGgqHviJHxOCEFEWZN6Z/IQKv5pO9gSdm/b+WfX+/dfheWhtZUyScqjlQ== dependencies: "@hapi/bourne" "^2.0.0" args "^5.0.1" - chalk "^3.0.0" + chalk "^4.0.0" dateformat "^3.0.3" fast-safe-stringify "^2.0.7" jmespath "^0.15.0" @@ -12696,14 +12701,14 @@ pino-pretty@^4.0.0: pump "^3.0.0" readable-stream "^3.6.0" split2 "^3.1.1" - strip-json-comments "^3.0.1" + strip-json-comments "^3.1.1" pino-std-serializers@^2.4.0, pino-std-serializers@^2.4.2: version "2.4.2" resolved "https://registry.yarnpkg.com/pino-std-serializers/-/pino-std-serializers-2.4.2.tgz#cb5e3e58c358b26f88969d7e619ae54bdfcc1ae1" integrity sha512-WaL504dO8eGs+vrK+j4BuQQq6GLKeCCcHaMB2ItygzVURcL1CycwNEUHTD/lHFHs/NL5qAz2UKrjYWXKSf4aMQ== -pino@^6.0.0, pino@^6.3.2: +pino@^6.0.0: version "6.3.2" resolved "https://registry.yarnpkg.com/pino/-/pino-6.3.2.tgz#55f73aa61584774ca5984068ffb78e8d519ce19e" integrity sha512-EiP3L1hoFw19KPocWimjnfXeysld0ne89ZRQ+bf8nAeA2TyuLoggNlibAi+Kla67GvQBopLdIZOsh1z/Lruo5Q== @@ -12715,6 +12720,18 @@ pino@^6.0.0, pino@^6.3.2: quick-format-unescaped "^4.0.1" sonic-boom "^1.0.0" +pino@^6.5.1: + version "6.7.0" + resolved "https://registry.yarnpkg.com/pino/-/pino-6.7.0.tgz#d5d96b7004fed78816b5694fda3eab02b5ca6d23" + integrity sha512-vPXJ4P9rWCwzlTJt+f0Ni4THc3DWyt8iDDCO4edQ8narTu6hnpzdXu8FqeSJCGndl1W6lfbYQUQihUO54y66Lw== + dependencies: + fast-redact "^3.0.0" + fast-safe-stringify "^2.0.7" + flatstr "^1.0.12" + pino-std-serializers "^2.4.2" + quick-format-unescaped "^4.0.1" + sonic-boom "^1.0.2" + pinpoint@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/pinpoint/-/pinpoint-1.1.0.tgz#0cf7757a6977f1bf7f6a32207b709e377388e874" @@ -14987,6 +15004,14 @@ sonic-boom@^1.0.0: atomic-sleep "^1.0.0" flatstr "^1.0.12" +sonic-boom@^1.0.2: + version "1.3.0" + resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-1.3.0.tgz#5c77c846ce6c395dddf2eb8e8e65f9cc576f2e76" + integrity sha512-4nX6OYvOYr6R76xfQKi6cZpTO3YSWe/vd+QdIfoH0lBy0MnPkeAbb2rRWgmgADkXUeCKPwO1FZAKlAVWAadELw== + dependencies: + atomic-sleep "^1.0.0" + flatstr "^1.0.12" + sort-keys@^1.0.0: version "1.1.2" resolved "https://registry.yarnpkg.com/sort-keys/-/sort-keys-1.1.2.tgz#441b6d4d346798f1b4e49e8920adfba0e543f9ad" @@ -15452,10 +15477,10 @@ strip-json-comments@^2.0.1, strip-json-comments@~2.0.1: resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" integrity sha1-PFMZQukIwml8DsNEhYwobHygpgo= -strip-json-comments@^3.0.1: - version "3.1.0" - resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.0.tgz#7638d31422129ecf4457440009fba03f9f9ac180" - integrity sha512-e6/d0eBu7gHtdCqFt0xJr642LdToM5/cN4Qb9DbHjVx1CP5RyeM+zH7pbecEmDv/lBqb0QH+6Uqq75rxFPkM0w== +strip-json-comments@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006" + integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig== style-loader@^0.23.1, style-loader@~0.23.1: version "0.23.1"