Bypass Cache on URL
Create an EdgeRule on the StackPath CDN to bypass the cache for certain URLs
This guide describes the process of creating a CDN EdgeRule to bypass the cache on a URL pattern match.
If you haven't yet check out our Getting Started Guide to see how to authenticate to the StackPath API and find your stack's ID.
In order to create an EdgeRule you will need the site's ID, stack ID, and a scope ID. Let's start by collecting your site scope's ID.
What is a site scope?
The StackPath API exposes a powerful CDN configuration system. Configurations are associated with a site's scopes. Scopes are a directory structure accessible over the CDN. For example, in the URL "http://www.example.org/blog/styles.css", "/" and "/blog" are both valid scopes where individual configuration policies can exist. These configurations affect delivery of the assets within those directories.
Every scope has a platform
and a path
attribute which tell the CDN the incoming HTTP request path to map to. For most use cases, configuration should be tied to a site's root CDS scope, or the scope with the path
value "/" and platform
value "CDS".
CDS stands for "Content Delivery Service", and is the name of the platform StackPath uses to indicate that a scope should be used for CDN site traffic.
Find the site's scope ID
The following API request retrieves all configuration scopes associated with a site. Replace STACK_ID
with your site's stack ID, SITE_ID
with your site's ID, and BEARER_TOKEN
with the authentication token generated by your StackPath user's API client ID and secret. Both IDs are UUIDv4 formatted strings, and the bearer token is a long hexadecimal string.
curl --request GET \
https://gateway.stackpath.com/cdn/v1/stacks/STACK_ID/sites/SITE_ID/scopes \
--header 'Authorization: bearer BEARER_TOKEN' \
--header 'Content-Type: application/json'
This request returns a JSON object that looks something like:
{
"pageInfo": {
"totalCount": "2",
"hasPreviousPage": false,
"hasNextPage": false,
"endCursor": "1"
},
"results": [
{
"id": "289820e0-d634-415c-9d19-b4493a377f82",
"platform": "ALL",
"path": "/"
},
{
"id": "a1e0fa90-ad12-48a2-a8f9-24807866a5ee",
"platform": "CDS",
"path": "/"
}
]
}
The site's root CDS scope is the results
value whose platform
value is "CDS" and path
value is "/". In this case, our root scope id is "a1e0fa90-ad12-48a2-a8f9-24807866a5ee".
Create the EdgeRule
The next API call tells the StackPath platform to create an EdgeRule to bypass the cache based on a specific URL filter. In this case, we will instruct the CDN not to cache content whose incoming request URL matches the wildcard value "https://www.example.org/test/*".
For this guide, we're only adding an originPullPolicy
configuration to the scope, but there are numerous other configuration options available to fine-tune your site's CDN delivery. Please check the API reference to see what else is possible.
curl --request POST \
--url https://gateway.stackpath.com/cdn/v1/stacks/STACK_ID/sites/SITE_ID/scopes/SCOPE_ID/rules \
--header 'accept: application/json' \
--header 'authorization: Bearer BEARERTOKEN' \
--header 'content-type: application/json' \
--data '
{
"name": "test rule",
"configuration": {
"originPullPolicy": [
{
"expirePolicy": "DO_NOT_CACHE",
"expireSeconds": 0,
"noCacheBehavior": "spec",
"enabled": true,
"pathFilter": "https://www.example.org/test/*"
}
]
}
}'
This request returns a JSON object containing the result of the rule that was created on the scope.
{
"rule": {
"id": "916c695b-115d-46e8-a874-a601be65aef7",
"name": "test rule",
"slug": "test-rule"
},
"configuration": {
"originPullPolicy": [
{
"id": "4985154720",
"expirePolicy": "DO_NOT_CACHE",
"expireSeconds": 0,
"noCacheBehavior": "spec",
"defaultCacheBehavior": "UNKNOWN",
"enabled": true,
"pathFilter": "https://www.example.org/test/*"
}
]
}
}
That's it! Your EdgeRule to bypass the cache on URLs that match https://www.mysite.com/test/* is in place and running on the StackPath platform. Any incoming requests that match the pattern will not be cached by the StackPath CDN.
If you have a request, comment, or just want to say hi please send an email. If you need immediate assistance our 24/7 support is here to help.
Updated over 3 years ago