Skip to content

Commit

Permalink
Cache SLAS callback using request processor (#884)
Browse files Browse the repository at this point in the history
* cache callback in request processor

* fix import path

* cache callback for a year

* use native URLSearchParams

* revert use native URLSearchParams
  • Loading branch information
kevinxh committed Jan 12, 2023
1 parent 6ef48f6 commit 3a2f185
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
29 changes: 17 additions & 12 deletions packages/template-retail-react-app/app/request-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// it processes requests in whatever way your project requires.

// Uncomment the following line for the example code to work.
// import {QueryParameters} from 'pwa-kit-react-sdk/utils/ssr-request-processing'
import {QueryParameters} from 'pwa-kit-runtime/utils/ssr-request-processing'

/**
* The processRequest function is called for *every* non-proxy, non-bundle
Expand Down Expand Up @@ -62,25 +62,29 @@ export const processRequest = ({
path,
querystring
}) => {
// Uncomment the snippet below for the example code to work.
/***************************************************************************
// Example code for filtering query parameters and detecting bots user.
console.assert(parameters, 'Missing parameters')
// This is an EXAMPLE processRequest implementation. You should
// replace it with code that processes your requests as needed.

// This example code will remove any of the parameters whose keys appear
// in the 'exclusions' array.
const exclusions = [
'gclid',
'utm_campaign',
'utm_content',
'utm_medium',
'utm_source'
// 'gclid',
// 'utm_campaign',
// 'utm_content',
// 'utm_medium',
// 'utm_source'
]

// This is a performance optimization for SLAS.
// On client side, browser always follow the redirect
// to /callback but the response is always the same.
// We strip out the unique query parameters so this
// endpoint is cached at the CDN level
if (path === '/callback') {
exclusions.push('usid')
exclusions.push('code')
}

// Build a first QueryParameters object from the given querystring
const incomingParameters = new QueryParameters(querystring)

Expand All @@ -96,6 +100,7 @@ export const processRequest = ({
// Re-generate the querystring
querystring = filteredParameters.toString()

/***************************************************************************
// This example code will detect bots by examining the user-agent,
// and will set the request class to 'bot' for all such requests.
const ua = headers.getHeader('user-agent')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ describe('processRequest', () => {
expect(result.path).toEqual(expect.any(String))
expect(result.querystring).toEqual(expect.any(String))
})

test('SLAS callback parameters are removed', () => {
const result = processRequest({path: '/callback', querystring: 'usid=1&code=2&test=3'})

expect(result.path).toEqual('/callback')
expect(result.querystring).toEqual('test=3')
})
})
3 changes: 3 additions & 0 deletions packages/template-retail-react-app/app/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const {handler} = runtime.createHandler(options, (app) => {

// Handle the redirect from SLAS as to avoid error
app.get('/callback?*', (req, res) => {
// This endpoint does nothing and is not expected to change
// Thus we cache it for a year to maximize performance
res.set('Cache-Control', `max-age=31536000`)
res.send()
})
app.get('/robots.txt', runtime.serveStaticFile('static/robots.txt'))
Expand Down

0 comments on commit 3a2f185

Please sign in to comment.