-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7607 from artsy/revert-7520-merch-ab-test
Revert "feat: setup AB test for a new default sort on the artist page"
- Loading branch information
Showing
13 changed files
with
199 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,18 @@ | ||
SplitTest = require './server_split_test.coffee' | ||
runningTests = require './running_tests' | ||
qs = require 'qs' | ||
sd = require('sharify').data | ||
|
||
module.exports = (req, res, next) -> | ||
for key, configuration of runningTests | ||
unless res.locals.sd[key?.toUpperCase()]? | ||
test = new SplitTest req, res, configuration | ||
outcome = test.outcome() | ||
res.locals.sd[key.toUpperCase()] = outcome | ||
sd[key.toUpperCase()] = outcome | ||
res.locals.sd[key.toUpperCase()] = test.outcome() | ||
|
||
if req.query?.split_test | ||
params = qs.parse req.query?.split_test | ||
for k, v of params | ||
test = new SplitTest req, res, runningTests[k] | ||
test.set v | ||
res.locals.sd[k.toUpperCase()] = v | ||
sd[k.toUpperCase()] = v | ||
|
||
next() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { pageCacheMiddleware } from "../pageCache" | ||
|
||
const { cache } = require("lib/cache") | ||
|
||
jest.mock("lib/cache", () => ({ | ||
cache: { | ||
get: jest.fn().mockResolvedValue(true), | ||
set: jest.fn(), | ||
}, | ||
})) | ||
|
||
jest.mock("config", () => ({ | ||
PAGE_CACHE_ENABLED: true, | ||
PAGE_CACHE_TYPES: "artist", | ||
PAGE_CACHE_NAMESPACE: "page-cache", | ||
PAGE_CACHE_VERSION: "1", | ||
PAGE_CACHE_EXPIRY_SECONDS: 600, | ||
PAGE_CACHE_RETRIEVAL_TIMEOUT_MS: 400, | ||
})) | ||
|
||
describe("pageCacheMiddleware", () => { | ||
let req | ||
let res | ||
let next | ||
beforeEach(() => { | ||
req = { | ||
path: "/artist/test-artist", | ||
url: "https://artsy.net/artist/test-artist", | ||
} | ||
res = { | ||
once: jest.fn((_e, cb) => cb()), | ||
locals: { | ||
PAGE_CACHE: { | ||
status: 200, | ||
key: "key", | ||
html: "html", | ||
}, | ||
}, | ||
} | ||
next = jest.fn() | ||
cache.get.mockClear() | ||
cache.set.mockClear() | ||
}) | ||
|
||
it("sets up cache for valid pageTypes", async () => { | ||
await pageCacheMiddleware(req, res, next) | ||
expect(cache.set).toBeCalledWith("page-cache|1|key", "html", 600) | ||
expect(cache.get.mock.calls[0][0]).toBe( | ||
"page-cache|1|https://artsy.net/artist/test-artist" | ||
) | ||
expect(next).toBeCalled() | ||
}) | ||
|
||
it("skips cache for invalid pageTypes", async () => { | ||
req = { | ||
path: "/artist-series/test-artist", | ||
url: "https://artsy.net/artist-series/test-artist", | ||
} | ||
await pageCacheMiddleware(req, res, next) | ||
expect(cache.set).not.toBeCalled() | ||
expect(cache.get).not.toBeCalled() | ||
expect(next).toBeCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import type { NextFunction } from "express" | ||
import type { ArtsyRequest, ArtsyResponse } from "./artsyExpress" | ||
|
||
import { getContextPageFromReq } from "lib/getContextPage" | ||
import { cache } from "lib/cache" | ||
import config from "../../config" | ||
|
||
const { | ||
PAGE_CACHE_ENABLED, | ||
PAGE_CACHE_EXPIRY_SECONDS, | ||
PAGE_CACHE_NAMESPACE, | ||
PAGE_CACHE_RETRIEVAL_TIMEOUT_MS, | ||
PAGE_CACHE_TYPES, | ||
PAGE_CACHE_VERSION, | ||
} = config | ||
|
||
const runningTests = Object.keys( | ||
require("desktop/components/split_test/running_tests.coffee") | ||
).sort() | ||
const cacheablePageTypes: string[] = PAGE_CACHE_TYPES.split("|") | ||
|
||
// Middleware will `next` and do nothing if any of the following is true: | ||
// | ||
// * page cache feature is disabled. | ||
// * a user is signed in. | ||
// * this isnt a supported cacheable path (there is an allow-list set in ENV). | ||
// * the page content is uncached. | ||
// * the cache errors. | ||
export async function pageCacheMiddleware( | ||
req: ArtsyRequest, | ||
res: ArtsyResponse, | ||
next: NextFunction | ||
) { | ||
if (!PAGE_CACHE_ENABLED) return next() | ||
|
||
// Returns true if the page type corresponding to `url` is configured cacheable. | ||
const isCacheablePageType = (req: ArtsyRequest) => { | ||
const { pageType } = getContextPageFromReq(req) | ||
|
||
return cacheablePageTypes.includes(pageType) | ||
} | ||
if (!isCacheablePageType(req)) return next() | ||
|
||
// @ts-ignore | ||
const hasUser = !!req.user | ||
if (hasUser) return next() | ||
|
||
// Generate cache key that includes all currently running AB tests and outcomes. | ||
const runningTestsCacheKey = runningTests | ||
.map(test => { | ||
const outcome = res.locals.sd[test.toUpperCase()] | ||
return `${test}:${outcome}` | ||
}) | ||
.join("|") | ||
|
||
// `key` should be a full URL w/ query params, and not a path. | ||
// This is to separate URL's like `/collect` and `/collect?acquireable=true`. | ||
const cacheKey = (key: string) => { | ||
return [PAGE_CACHE_NAMESPACE, runningTestsCacheKey, PAGE_CACHE_VERSION, key] | ||
.filter(Boolean) | ||
.join("|") | ||
} | ||
|
||
// `key` is the full URL. | ||
const cacheHtmlForPage = ({ status, key, html }) => { | ||
if (status !== 200) return | ||
|
||
cache.set(cacheKey(key), html, PAGE_CACHE_EXPIRY_SECONDS) | ||
} | ||
|
||
const cacheKeyForRequest = cacheKey(req.url) | ||
|
||
// Register callback to write rendered page data to cache. | ||
res.once("finish", () => { | ||
if (res.locals.PAGE_CACHE) { | ||
// eslint-disable-next-line no-console | ||
console.log(`[Page Cache]: Writing ${cacheKeyForRequest} to cache`) | ||
cacheHtmlForPage(res.locals.PAGE_CACHE) | ||
} | ||
}) | ||
|
||
try { | ||
await new Promise<void>((resolve, reject) => { | ||
// Cache timeout handler, will reject if hit. | ||
let timeoutId = setTimeout(() => { | ||
// @ts-expect-error STRICT_NULL_CHECK | ||
timeoutId = null | ||
const error = new Error( | ||
`Timeout of ${PAGE_CACHE_RETRIEVAL_TIMEOUT_MS}ms, skipping...` | ||
) | ||
reject(error) | ||
}, PAGE_CACHE_RETRIEVAL_TIMEOUT_MS) | ||
|
||
const handleCacheGet = (_err, html) => { | ||
if (!timeoutId) return // Already timed out. | ||
|
||
clearTimeout(timeoutId) | ||
|
||
if (html) { | ||
// eslint-disable-next-line no-console | ||
console.log(`[Page Cache]: Reading ${cacheKeyForRequest} from cache`) | ||
return res.send(html) | ||
} | ||
|
||
resolve() | ||
} | ||
|
||
cache.get(cacheKey(req.url), handleCacheGet) | ||
}) | ||
} catch (e) { | ||
console.error(`[Page Cache Middleware]: ${e.message}`) | ||
} finally { | ||
next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.