Skip to content

Commit

Permalink
fix: surrogate key for non-existing .html in code-bus
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-guggisberg committed Jan 24, 2025
1 parent 172e010 commit c10182c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
11 changes: 6 additions & 5 deletions src/json-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { cleanupHeaderValue, computeSurrogateKey } from '@adobe/helix-shared-utils';
import initConfig from './steps/init-config.js';
import setCustomResponseHeaders from './steps/set-custom-response-headers.js';
import { computeContentPathKey, computeCodePathKey } from './steps/set-x-surrogate-key-header.js';
import { PipelineResponse } from './PipelineResponse.js';
import jsonFilter from './utils/json-filter.js';
import { extractLastModified, updateLastModified } from './utils/last-modified.js';
Expand Down Expand Up @@ -42,7 +43,7 @@ export default function folderMapping(state) {

async function fetchJsonContent(state, req, res) {
const {
owner, repo, ref, contentBusId, partition, s3Loader, log, info,
owner, repo, ref, contentBusId, partition, s3Loader, log,
} = state;
const { path } = state.info;
state.content.sourceBus = 'content';
Expand All @@ -65,11 +66,11 @@ async function fetchJsonContent(state, req, res) {
if (state.content.sourceBus === 'content') {
// provide either (prefixed) preview or (unprefixed) live content keys
const contentKeyPrefix = partition === 'preview' ? 'p_' : '';
keys.push(`${contentKeyPrefix}${await computeSurrogateKey(`${contentBusId}${info.path}`)}`);
keys.push(`${contentKeyPrefix}${await computeContentPathKey(state)}`);
keys.push(`${contentKeyPrefix}${contentBusId}`);
} else {
keys.push(`${ref}--${repo}--${owner}_code`);
keys.push(await computeSurrogateKey(`${ref}--${repo}--${owner}${info.path}`));
keys.push(await computeCodePathKey(state));
}
res.headers.set('x-surrogate-key', keys.join(' '));
res.error = 'moved';
Expand Down Expand Up @@ -100,13 +101,13 @@ async function computeSurrogateKeys(state) {
keys.push(await computeSurrogateKey(`${state.site}--${state.org}_config.json`));
}
if (state.content.sourceBus.includes('code')) {
keys.push(await computeSurrogateKey(`${state.ref}--${state.repo}--${state.owner}${state.info.path}`));
keys.push(await computeCodePathKey(state));
keys.push(`${state.ref}--${state.repo}--${state.owner}_code`);
}
if (state.content.sourceBus.includes('content')) {
// provide either (prefixed) preview or (unprefixed) live content keys
const contentKeyPrefix = state.partition === 'preview' ? 'p_' : '';
keys.push(`${contentKeyPrefix}${await computeSurrogateKey(`${state.contentBusId}${state.info.path}`)}`);
keys.push(`${contentKeyPrefix}${await await computeContentPathKey(state)}`);
keys.push(`${contentKeyPrefix}${state.contentBusId}`);
}

Expand Down
4 changes: 2 additions & 2 deletions src/robots-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
import { cleanupHeaderValue, computeSurrogateKey } from '@adobe/helix-shared-utils';
import renderCode from './steps/render-code.js';
import { computeCodePathKey } from './steps/set-x-surrogate-key-header.js';
import setCustomResponseHeaders from './steps/set-custom-response-headers.js';
import { PipelineResponse } from './PipelineResponse.js';
import initConfig from './steps/init-config.js';
Expand Down Expand Up @@ -119,9 +120,8 @@ function getForwardedHosts(req) {
async function computeSurrogateKeys(state) {
const keys = [];

const pathKey = `${state.ref}--${state.repo}--${state.owner}${state.info.path}`;
keys.push(await computeSurrogateKey(`${state.site}--${state.org}_config.json`));
keys.push(await computeSurrogateKey(pathKey));
keys.push(await computeCodePathKey(state));
return keys;
}

Expand Down
22 changes: 13 additions & 9 deletions src/steps/fetch-404.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/
import { extractLastModified } from '../utils/last-modified.js';
import { getPathKey } from './set-x-surrogate-key-header.js';
import { computeContentPathKey, computeCodePathKey } from './set-x-surrogate-key-header.js';

/**
* Loads the 404.html from code-bus and stores it in `res.body`
Expand Down Expand Up @@ -39,18 +39,22 @@ export default async function fetch404(state, req, res) {
}

// set 404 keys in any case
const pathKey = await getPathKey(state);
// provide either (prefixed) preview or (unprefixed) live content keys
const contentKeyPrefix = partition === 'preview' ? 'p_' : '';
const keys = [
`${contentKeyPrefix}${pathKey}`,
`${contentKeyPrefix}${contentBusId}`,
`${ref}--${repo}--${owner}_404`,
`${ref}--${repo}--${owner}_code`,
];
const keys = [];
if (state.content.sourceBus === 'code') {
keys.push(await computeCodePathKey(state));
} else {
const contentPathKey = await computeContentPathKey(state);
// provide either (prefixed) preview or (unprefixed) live content keys
keys.push(`${contentKeyPrefix}${contentPathKey}`);
keys.push(`${contentKeyPrefix}${contentBusId}`);
}
keys.push(`${ref}--${repo}--${owner}_404`);
keys.push(`${ref}--${repo}--${owner}_code`);

if (state.info.unmappedPath) {
const unmappedPathKey = await getPathKey({
const unmappedPathKey = await computeContentPathKey({
contentBusId,
info: { path: state.info.unmappedPath },
});
Expand Down
6 changes: 3 additions & 3 deletions src/steps/fetch-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { computeSurrogateKey } from '@adobe/helix-shared-utils';
import { extractLastModified, updateLastModified } from '../utils/last-modified.js';
import { computeContentPathKey, computeCodePathKey } from './set-x-surrogate-key-header.js';

/**
* Loads the content from either the content-bus or code-bus and stores it in `state.content`
Expand Down Expand Up @@ -43,12 +43,12 @@ export default async function fetchContent(state, req, res) {
res.headers.set('location', redirectLocation);
const keys = [];
if (isCode) {
keys.push(await computeSurrogateKey(`${ref}--${repo}--${owner}${info.path}`));
keys.push(await computeCodePathKey(state));
keys.push(`${ref}--${repo}--${owner}_code`);
} else {
// provide either (prefixed) preview or (unprefixed) live content keys
const contentKeyPrefix = partition === 'preview' ? 'p_' : '';
keys.push(`${contentKeyPrefix}${await computeSurrogateKey(`${contentBusId}${info.path}`)}`);
keys.push(`${contentKeyPrefix}${await computeContentPathKey(state)}`);
keys.push(`${contentKeyPrefix}${contentBusId}`);
}
res.headers.set('x-surrogate-key', keys.join(' '));
Expand Down
24 changes: 19 additions & 5 deletions src/steps/set-x-surrogate-key-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import { computeSurrogateKey } from '@adobe/helix-shared-utils';

/**
* Returns the surrogate key based on the contentBusId and the resource path
* Returns the surrogate key for a content-bus resource
* based on the contentBusId and the resource path
* @param state
* @returns {Promise<string>}
*/
export async function getPathKey(state) {
export async function computeContentPathKey(state) {
const { contentBusId, info } = state;
let { path } = info;
// surrogate key for path
Expand All @@ -33,6 +34,19 @@ export async function getPathKey(state) {
return computeSurrogateKey(`${contentBusId}${path}`);
}

/**
* Returns the surrogate key for a code-bus resource
* based on the repositry and the resource path
* @param state
* @returns {Promise<string>}
*/
export async function computeCodePathKey(state) {
const {
owner, repo, ref, info: { path },
} = state;
return computeSurrogateKey(`${ref}--${repo}--${owner}${path}`);
}

/**
* @type PipelineStep
* @param {PipelineState} state
Expand All @@ -50,9 +64,9 @@ export default async function setXSurrogateKeyHeader(state, req, res) {
// provide either (prefixed) preview or (unprefixed) live content keys
const contentKeyPrefix = partition === 'preview' ? 'p_' : '';
const keys = [];
const hash = await getPathKey(state);
const hash = await computeContentPathKey(state);
if (isCode) {
keys.push(await computeSurrogateKey(`${ref}--${repo}--${owner}${state.info.path}`));
keys.push(await computeCodePathKey(state));
keys.push(`${ref}--${repo}--${owner}_code`);
} else {
keys.push(`${contentKeyPrefix}${hash}`);
Expand All @@ -64,7 +78,7 @@ export default async function setXSurrogateKeyHeader(state, req, res) {
if (state.mapped) {
keys.push(`${contentKeyPrefix}${hash}_metadata`);
if (state.info.unmappedPath) {
keys.push(`${contentKeyPrefix}${await getPathKey({
keys.push(`${contentKeyPrefix}${await computeContentPathKey({
contentBusId,
info: { path: state.info.unmappedPath },
})}`);
Expand Down
2 changes: 1 addition & 1 deletion test/rendering.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ describe('Rendering', () => {
'content-type': 'text/html; charset=utf-8',
'last-modified': 'Wed, 12 Oct 2009 17:50:00 GMT',
'x-error': 'failed to load /not-found-with-handler.html from code-bus: 404',
'x-surrogate-key': 'ta3V7wR3zlRh1b0E foo-id super-test--helix-pages--adobe_404 super-test--helix-pages--adobe_code',
'x-surrogate-key': '9q9qs7DEYGc4lYTJ super-test--helix-pages--adobe_404 super-test--helix-pages--adobe_code',
link: '</scripts/scripts.js>; rel=modulepreload; as=script; crossorigin=use-credentials',
'access-control-allow-origin': '*',
});
Expand Down

0 comments on commit c10182c

Please sign in to comment.