Skip to content

Added cache control for http request within BrowserClient #1706

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

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9ba5471
Update README.md
seifibrahim32 Mar 13, 2024
ee01325
Merge branch 'dart-lang:master' into master
seifibrahim32 Jan 31, 2025
3a26def
Added caching options for http package
seifibrahim32 Feb 2, 2025
3ba65df
Enabled lints
seifibrahim32 Feb 2, 2025
f5c9c11
Fixed CI errors
seifibrahim32 Feb 2, 2025
52ff2c5
Changed from HttpCacheUtils to HttpCacheOptions
seifibrahim32 Feb 2, 2025
7f70434
Reverted readme file
seifibrahim32 Feb 3, 2025
ae7be97
Reverted to last commits for request files
seifibrahim32 Feb 3, 2025
1ca0c3a
Reformated the committed files
seifibrahim32 Feb 3, 2025
7a5c30d
Changed values
seifibrahim32 Feb 3, 2025
85daade
Passing CI stages
seifibrahim32 Feb 3, 2025
7ece5aa
Changed the parameter to String type
seifibrahim32 Feb 4, 2025
4bca978
Reverted utils.dart
seifibrahim32 Feb 5, 2025
dad889c
Changed CacheMode from utils to browser_client file
seifibrahim32 Feb 5, 2025
2889cb2
Enhanced BrowserClient class
seifibrahim32 Feb 5, 2025
3d17957
Formatted for CI passover
seifibrahim32 Feb 5, 2025
a1f47db
Export CacheMode
seifibrahim32 Feb 6, 2025
cab59fc
Added some cache tests
seifibrahim32 Feb 11, 2025
5831578
Reverted utils.dart
seifibrahim32 Feb 11, 2025
2fd11c1
Reverted stub server
seifibrahim32 Feb 11, 2025
53dc82f
Add tests cache including fixing CI passover
seifibrahim32 Feb 12, 2025
1adce3e
Updated the branch fork
seifibrahim32 Feb 16, 2025
7d50328
Update browser_client with formatted style
seifibrahim32 Feb 16, 2025
410d227
Added for tests branch
seifibrahim32 Feb 19, 2025
83672f8
Added browser clients tests including test fixes
seifibrahim32 Mar 10, 2025
40609bc
Updated pubspec.yaml file
seifibrahim32 Mar 10, 2025
ff7f7e8
Reset the server port to zero
seifibrahim32 Mar 10, 2025
a63d6ce
Fix the dart.yml
seifibrahim32 Mar 10, 2025
7e1bc9f
Fixed WASM runtime
seifibrahim32 Mar 10, 2025
0eabc8d
Fixed WASM runtime v2
seifibrahim32 Mar 10, 2025
c144307
Fixed WASM runtime v4
seifibrahim32 Mar 10, 2025
34a87fb
Fixed WASM runtime v5
seifibrahim32 Mar 10, 2025
5294e96
Fixed WASM runtime v5
seifibrahim32 Mar 10, 2025
d12c0a4
Update CHANGELOG.md
seifibrahim32 Mar 10, 2025
3c53220
Update pubspec.yaml
seifibrahim32 Mar 10, 2025
b485213
Update CHANGELOG.md
seifibrahim32 Mar 10, 2025
f65dd4f
Update cache_test.dart
seifibrahim32 Mar 10, 2025
42251ea
Changed POST to GET and ensured of some tests
seifibrahim32 Mar 10, 2025
4c7471f
Added etags and cache control
seifibrahim32 Mar 11, 2025
4896114
Fixed conflicts
seifibrahim32 Mar 12, 2025
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
19 changes: 18 additions & 1 deletion pkgs/http/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'dart:async';
import 'dart:js_interop';

import 'package:web/web.dart'
show
AbortController,
Expand Down Expand Up @@ -45,6 +44,18 @@ BaseClient createClient() {
class BrowserClient extends BaseClient {
final _abortController = AbortController();

/// Create a new browser-based HTTP Client.
///
/// If [cacheMode] is provided then it can be used to cache the request
/// in the browser.
///
/// For example:
/// ```dart
/// const mode = 'reload';
/// final client = BrowserClient(cacheMode: mode);
/// ```
BrowserClient({this.cacheMode = 'default'});

/// Whether to send credentials such as cookies or authorization headers for
/// cross-site requests.
///
Expand All @@ -53,6 +64,11 @@ class BrowserClient extends BaseClient {

bool _isClosed = false;

/// Use different caching mode for a HTTP request.
/// Defaults to `default`.
// https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
String cacheMode;

/// Sends an HTTP request and asynchronously returns the response.
@override
Future<StreamedResponse> send(BaseRequest request) async {
Expand All @@ -69,6 +85,7 @@ class BrowserClient extends BaseClient {
RequestInit(
method: request.method,
body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null,
cache: cacheMode,
credentials: withCredentials ? 'include' : 'same-origin',
headers: {
if (request.contentLength case final contentLength?)
Expand Down
19 changes: 19 additions & 0 deletions pkgs/http/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,22 @@ Stream<T> onDone<T>(Stream<T> stream, void Function() onDone) =>
sink.close();
onDone();
}));

/// Caching modes for any http request in a browser.
enum CacheMode {
defaultType('default'),
reload('reload'),
noStore('no-store'),
noCache('no-cache'),
forceCache('force-cache'),
onlyIfCached('only-if-cached');

final String cacheType;

const CacheMode(this.cacheType);

static CacheMode fromString(String cacheType) => values.firstWhere(
(v) => v.cacheType == cacheType,
orElse: () => CacheMode.defaultType,
);
}