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

Implement Redirect operation - Pre-UA and On-UA implementations #17

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sdks/npm/src/cf/on-ua-applicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ export default class UATransformApplicator implements HTMLRewriterElementConten
}

static serializeOperation(operation: Experimentation.Operations, args: any[]) {
if (operation === Experimentation.Operations.CustomJs) {
return [operation, `$=>{${args[0]}}`];
switch (operation) {
case Experimentation.Operations.CustomJs:
return [operation, `$=>{${args[0]}}`];
case Experimentation.Operations.Redirect:
// In case of a redirect, we use a CustomJs operation to rewrite location.
return [Experimentation.Operations.CustomJs, `$=>{window.location.href="${JSON.stringify(args[0])}"}`];
Copy link
Collaborator

Choose a reason for hiding this comment

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

We'll need to validate our args eventually, for all of our operations, for both PRE and ON UA. I don't think that's necessary for this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Added this ticket to resolve this: #20

}
return [operation, ...args.map(arg => JSON.stringify(arg))];
}
Expand Down
6 changes: 6 additions & 0 deletions sdks/npm/src/cf/pre-ua-applicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@ export default class PreUATransformApplicator implements HTMLRewriterElementCont
}

// Handle any complex operations that involves multiple CF operations.
switch (this.op) {
case Experimentation.Operations.Redirect:
const [url, code] = this.args;
Response.redirect(url, code);
break;
}
}
}
88 changes: 30 additions & 58 deletions specs/EXPLAINER.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,125 +109,97 @@ where each field can be defined as follows
<td style="background-color: #d9d9d9">Operation</td>
<td style="background-color: #d9d9d9">Code</td>
<td style="background-color: #d9d9d9">Description</td>
<td style="background-color: #d9d9d9">Arguments</td>
</tr>
<tr>
<td>OP_CUSTOM_JS</td>
<td>0</td>
<td>Executes a custom Javascript block of code against the element.
<p>

Arguments:

`code`: Javascript code serialized as a string. The applicator code will call this code as a function ($) => code, $ referring to the element selected.
<td>

`code` : Javascript code serialized as a string. The applicator code will call this code as a function ($) => code, $ referring to the element selected.
</td>
</tr>
<tr>
<td>OP_INSERT_BEFORE</td>
<td>1</td>
<td>Inserts content right before the element.
<p>

Arguments:
<td>Inserts content right before the element.</td>
<td>

`content`: HTML markup
</td>
</tr>
<tr>
<td>OP_INSERT_AFTER</td>
<td>2</td>
<td>Inserts content right after the element.
<p>
<td>Inserts content right after the element.</td>
<td>

Arguments:

<p>
`content`: HTML markup
</td>
</tr>
<tr>
<td>OP_PREPEND</td>
<td>3</td>
<td>Inserts content right after the start tag of the element.
<p>

Arguments:

<p>
`content`: HTML markup
<td>Inserts content right after the start tag of the element.</td>
<td>

`content`: HTML markup
</td>
</tr>
<tr>
<td>OP_APPEND</td>
<td>4</td>
<td>Inserts content right before the end tag of the element.
<p>

Arguments:
<td>Inserts content right before the end tag of the element.</td>
<td>

<p>
`content`: HTML markup
</td>
</tr>
<tr>
<td>OP_REPLACE</td>
<td>5</td>
<td>Replaces the element with the provided content.
<p>
<td>Replaces the element with the provided content.</td>
<td>

Arguments:

<p>
`content`: HTML markup
</td>
</tr>
<tr>
<td>OP_SET_INNERHTML</td>
<td>6</td>
<td>Replaces the content of the element with provided content.
<p>

Arguments:

<p>
<td>Replaces the content of the element with provided content.</td>
<td>

`content`: HTML markup
</td>
</tr>
<tr>
<td>OP_REMOVE</td>
<td>7</td>
<td>Removes the element and its children
<p>

Arguments:

<p>
none.
</td>
<td>Removes the element and its children</td>
<td>none</td>
</tr>
<tr>
<td>OP_SET_ATTRIBUTE</td>
<td>8</td>
<td>Sets an attribute’s value on the element.
<p>

Arguments:

<p>
<td>Sets an attribute’s value on the element.</td>
<td>

`name`: Name of the attribute
<p>

`value`: Value to be set on the attribute.
</td>
</tr>
<tr>
<td>OP_REDIRECT</td>
<td>9</td>
<td>Redirect the user to a different page or URL.
<p>

Arguments:

<p>
<td>Redirect the user to a different page or URL. During `PRE_UA` phase, this will perform an HTTP redirect and during `ON_UA`, this will result in client side redirection. Additional arguments supplied can select the response code during PRE_UA phase.</td>
<td>

`URL`: URL to redirect the page to.

`code`: HTTP code to use for redirection during PRE_UA phase.
</td>
</tr>
</table>
Expand Down