Skip to content

Commit

Permalink
Merge pull request #423 from xylabs/feature/puppeteer-performance
Browse files Browse the repository at this point in the history
Dynamic Share Error Responses
  • Loading branch information
JoelBCarter authored Aug 8, 2024
2 parents 425e125 + 9b21b22 commit 6483b9e
Show file tree
Hide file tree
Showing 33 changed files with 228 additions and 271 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"args": [
"--forceExit",
"--runInBand",
"src/modules/metaServer/lib/page/usePage/spec/useSpaPage.spec.ts",
"src/modules/metaServer/contentHandlers/liveShare/spec/liveShare.spec.ts",
],
"pauseForSourceMap": true,
"console": "integratedTerminal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Path from 'node:path'
import { assertEx } from '@xylabs/assert'
import { exists } from '@xylabs/exists'
import { asyncHandler } from '@xylabs/sdk-api-express-ecs'
import { HttpStatusCode } from 'axios'
import { RequestHandler } from 'express'

import {
Expand All @@ -26,7 +27,7 @@ import { useIndexAndDynamicPreviewImage } from './lib/index.js'
const indexHtmlMaxAge = 60 * 10
const indexHtmlCacheControlHeader = `public, max-age=${indexHtmlMaxAge}`

const disableCaching = false
const enableCaching = false

const getPageHandler = (baseDir: string) => {
// Ensure file containing base HTML exists
Expand All @@ -39,28 +40,36 @@ const getPageHandler = (baseDir: string) => {
const pageHandler: RequestHandler = asyncHandler(async (req, res, next) => {
const adjustedPath = getAdjustedPath(req)
if (Path.extname(adjustedPath) === '.html') {
const uri = getUriBehindProxy(req)
try {
const uri = getUriBehindProxy(req)
console.log(`[dynamicShare][pageHandler][${uri}]: called`)
const cachedHtml = await pageRepository.findFile(adjustedPath)
if (cachedHtml && !disableCaching) {
console.log(`[dynamicShare][pageHandler][${uri}]: return cached`)
const html = arrayBufferToString(await cachedHtml.data)
res.type('html').set('Cache-Control', indexHtmlCacheControlHeader).send(html)
return
} else {
console.log(`[dynamicShare][pageHandler][${uri}]: rendering`)
const updatedHtml = await useIndexAndDynamicPreviewImage(uri, indexHtml)
console.log(`[dynamicShare][pageHandler][${uri}]: caching`)
const data = stringToArrayBuffer(updatedHtml)
const file: RepositoryFile = { data, type: 'text/html', uri: adjustedPath }
await pageRepository.addFile(file)
console.log(`[dynamicShare][pageHandler][${uri}]: return html`)
res.type('html').set('Cache-Control', indexHtmlCacheControlHeader).send(updatedHtml)
return
if (enableCaching) {
console.log(`[dynamicShare][pageHandler][${uri}]: checking for cached`)
const cachedHtml = await pageRepository.findFile(adjustedPath)
if (cachedHtml) {
console.log(`[dynamicShare][pageHandler][${uri}]: return cached`)
const html = arrayBufferToString(await cachedHtml.data)
res.type('html').set('Cache-Control', indexHtmlCacheControlHeader).send(html)
return
}
}
console.log(`[dynamicShare][pageHandler][${uri}]: rendering`)
const updatedHtml = await useIndexAndDynamicPreviewImage(uri, indexHtml)
console.log(`[dynamicShare][pageHandler][${uri}]: caching`)
const data = stringToArrayBuffer(updatedHtml)
const file: RepositoryFile = { data, type: 'text/html', uri: adjustedPath }
await pageRepository.addFile(file)
console.log(`[dynamicShare][pageHandler][${uri}]: return html`)
res.type('html').set('Cache-Control', indexHtmlCacheControlHeader).send(updatedHtml)
return
} catch (error) {
const status = HttpStatusCode.ServiceUnavailable
console.log(`[dynamicShare][useIndexAndDynamicPreviewImage][${uri}]: error, returning status code ${status}`)
console.log(error)
res.status(status)
.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate')
.set('Retry-After', '60') // Retry after 60 seconds
.send()
}
}
next()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, OpenGraphMeta, TwitterMeta } from '@xyo-network/sdk-meta'

import { defaultViewportSize } from '../../../../lib/index.js'
import { defaultViewportSize } from '../../../../lib/index.ts'
import { getImageUrlFromPage } from './getImageUrlFromPage.ts'

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './getImageMeta.js'
export * from './getImageMeta.ts'

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './getRenderedPageHtml/index.js'
export * from './useIndexAndDeferredPreviewImage/index.ts'
export * from './useIndexAndDynamicPreviewImage/index.ts'

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { metaBuilder } from '@xyo-network/sdk-meta'

import { getImageMeta } from '../../image/index.ts'

export const useIndexAndDynamicPreviewImage = async (url: string, indexHtml: string): Promise<string> => {
console.log(`[dynamicShare][useIndexAndDynamicPreviewImage][${url}]: generating preview image meta`)
const meta = await getImageMeta(url)
const updatedHtml = metaBuilder(indexHtml, meta)
console.log(`[dynamicShare][useIndexAndDynamicPreviewImage][${url}]: returning index.html & preview image meta`)
return updatedHtml
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './getRenderedPageHtml/index.js'
export * from './useIndexAndDeferredPreviewImage/index.js'
export * from './useIndexAndDeferredPreviewImage/index.ts'
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const maxImageGenerationWait = 8000
* the environment is fully initialized.
*/
const imageRepository = () => getFileRepository()
const disableCaching = false
const enableCaching = true

const getPageHandler = (baseDir: string) => {
// Ensure file containing base HTML exists
Expand All @@ -70,23 +70,25 @@ const getPageHandler = (baseDir: string) => {
try {
const uri = getUriBehindProxy(req)
console.log(`[liveShare][pageHandler][${uri}]: called`)
const cachedHtml = await pageRepository.findFile(adjustedPath)
if (cachedHtml && !disableCaching) {
console.log(`[liveShare][pageHandler][${uri}]: return cached`)
const html = arrayBufferToString(await cachedHtml.data)
res.type('html').set('Cache-Control', indexHtmlCacheControlHeader).send(html)
return
} else {
console.log(`[liveShare][pageHandler][${uri}]: rendering`)
const updatedHtml = useIndexAndDeferredPreviewImage(uri, imageRepository(), indexHtml)
console.log(`[liveShare][pageHandler][${uri}]: caching`)
const data = stringToArrayBuffer(updatedHtml)
const file: RepositoryFile = { data, type: 'text/html', uri: adjustedPath }
await pageRepository.addFile(file)
console.log(`[liveShare][pageHandler][${uri}]: return html`)
res.type('html').set('Cache-Control', indexHtmlCacheControlHeader).send(updatedHtml)
return
if (enableCaching) {
console.log(`[liveShare][pageHandler][${uri}]: checking for cached`)
const cachedHtml = await pageRepository.findFile(adjustedPath)
if (cachedHtml) {
console.log(`[liveShare][pageHandler][${uri}]: return cached`)
const html = arrayBufferToString(await cachedHtml.data)
res.type('html').set('Cache-Control', indexHtmlCacheControlHeader).send(html)
return
}
}
console.log(`[liveShare][pageHandler][${uri}]: rendering`)
const updatedHtml = useIndexAndDeferredPreviewImage(uri, imageRepository(), indexHtml)
console.log(`[liveShare][pageHandler][${uri}]: caching`)
const data = stringToArrayBuffer(updatedHtml)
const file: RepositoryFile = { data, type: 'text/html', uri: adjustedPath }
await pageRepository.addFile(file)
console.log(`[liveShare][pageHandler][${uri}]: return html`)
res.type('html').set('Cache-Control', indexHtmlCacheControlHeader).send(updatedHtml)
return
} catch (error) {
console.log(error)
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/metaServer/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './file/index.js'
export * from './head/index.js'
export * from './image/index.js'
export * from './page/index.js'
export * from './pageHtml/index.js'
export * from './path/index.js'
export * from './repository/index.js'
export * from './socialMedia/index.js'
Expand Down
1 change: 1 addition & 0 deletions src/modules/metaServer/lib/logging/ScopedLogFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ScopedLogFunction = (message: string, scopes: string[]) => void
2 changes: 2 additions & 0 deletions src/modules/metaServer/lib/logging/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ScopedLogFunction.ts'
export * from './scopedLoggers.ts'
Loading

0 comments on commit 6483b9e

Please sign in to comment.