Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
frontend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
videoag
frontend
Commits
462d7336
Verified
Commit
462d7336
authored
10 months ago
by
Dorian Koch
Browse files
Options
Downloads
Patches
Plain Diff
Begin smart caching
parent
cdfd1821
No related branches found
No related tags found
No related merge requests found
Pipeline
#6379
passed
10 months ago
Stage: test
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/api/Backend.tsx
+40
-1
40 additions, 1 deletion
src/api/Backend.tsx
src/components/BackendProvider.tsx
+3
-3
3 additions, 3 deletions
src/components/BackendProvider.tsx
with
43 additions
and
4 deletions
src/api/Backend.tsx
+
40
−
1
View file @
462d7336
...
@@ -67,6 +67,10 @@ export class BackendImpl {
...
@@ -67,6 +67,10 @@ export class BackendImpl {
private
csrfToken
?:
string
;
private
csrfToken
?:
string
;
public
isPrivileged
=
false
;
public
isPrivileged
=
false
;
protected
fetchImpl
(
url
:
string
,
init
?:
RequestInit
):
Promise
<
Response
>
{
return
fetch
(
url
,
init
);
}
assetUrl
():
string
{
assetUrl
():
string
{
return
"
https://video.fsmpi.rwth-aachen.de/files
"
;
return
"
https://video.fsmpi.rwth-aachen.de/files
"
;
}
}
...
@@ -135,7 +139,7 @@ export class BackendImpl {
...
@@ -135,7 +139,7 @@ export class BackendImpl {
init
.
cache
=
"
no-cache
"
;
init
.
cache
=
"
no-cache
"
;
}
}
init
.
credentials
=
"
include
"
;
init
.
credentials
=
"
include
"
;
return
fetch
(
url
,
init
);
return
this
.
fetch
Impl
(
url
,
init
);
}
}
setCsrfToken
(
csrfToken
?:
string
)
{
setCsrfToken
(
csrfToken
?:
string
)
{
...
@@ -505,3 +509,38 @@ export class BackendImpl {
...
@@ -505,3 +509,38 @@ export class BackendImpl {
});
});
}
}
}
}
export
class
SmartCacheBackend
extends
BackendImpl
{
protected
fetchImpl
(
url
:
string
,
init
?:
RequestInit
)
{
let
shouldCache
=
true
;
if
(
!
SmartCacheBackend
.
isSupported
())
shouldCache
=
false
;
if
(
init
&&
init
.
cache
===
"
no-cache
"
)
shouldCache
=
false
;
if
(
!
url
.
startsWith
(
this
.
baseUrl
()))
shouldCache
=
false
;
if
(
!
shouldCache
)
return
super
.
fetchImpl
(
url
,
init
);
const
cacheName
=
`api-v1-
${
this
.
isPrivileged
?
"
privileged
"
:
"
public
"
}
`
;
return
caches
.
open
(
cacheName
).
then
((
cache
)
=>
{
return
cache
.
match
(
url
).
then
((
response
)
=>
{
if
(
response
)
{
console
.
log
(
`Cache hit (
${
cacheName
}
) on
${
url
}
`
);
return
response
;
}
else
{
return
fetch
(
url
,
init
).
then
((
response
)
=>
{
cache
.
put
(
url
,
response
.
clone
());
return
response
;
});
}
});
});
}
static
isSupported
()
{
return
window
.
isSecureContext
&&
"
caches
"
in
window
;
}
}
export
class
InvalidBackend
extends
BackendImpl
{
protected
fetchImpl
(
url
:
string
,
init
?:
RequestInit
):
Promise
<
Response
>
{
return
Promise
.
reject
(
new
Error
(
"
Invalid backend
"
));
}
}
This diff is collapsed.
Click to expand it.
src/components/BackendProvider.tsx
+
3
−
3
View file @
462d7336
import
{
Backend
Impl
}
from
"
@/api/Backend
"
;
import
{
Backend
,
InvalidBackend
,
SmartCacheBackend
}
from
"
@/api/Backend
"
;
import
{
createContext
,
useContext
,
useState
}
from
"
react
"
;
import
{
createContext
,
useContext
,
useState
}
from
"
react
"
;
import
type
React
from
"
react
"
;
import
type
React
from
"
react
"
;
const
BackendContext
=
createContext
<
Backend
Impl
>
(
new
Backend
Impl
());
const
BackendContext
=
createContext
<
Backend
>
(
new
Invalid
Backend
());
export
function
RealBackendProvider
({
children
}:
{
children
:
React
.
ReactNode
})
{
export
function
RealBackendProvider
({
children
}:
{
children
:
React
.
ReactNode
})
{
let
[
state
,
_
]
=
useState
(()
=>
new
Backend
Impl
());
let
[
state
,
_
]
=
useState
(()
=>
new
SmartCache
Backend
());
return
<
BackendContext
.
Provider
value
=
{
state
}
>
{
children
}
</
BackendContext
.
Provider
>;
return
<
BackendContext
.
Provider
value
=
{
state
}
>
{
children
}
</
BackendContext
.
Provider
>;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment