Skip to content

Commit 3cb7a3e

Browse files
authored
fix(cli/devserver): inject autoreload into HTML only, closes #6997 (#7032)
1 parent baa581b commit 3cb7a3e

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'tauri-cli': 'patch'
3+
'@tauri-apps/cli': 'patch'
4+
---
5+
6+
Fix built-in devserver adding hot-reload code to non-html files.

.changes/mime-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri-utils': 'patch'
3+
---
4+
5+
Add `MimeType::parse_with_fallback` and `MimeType::parse_from_uri_with_fallback`

core/tauri-utils/src/mime_type.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ impl std::fmt::Display for MimeType {
4646
impl MimeType {
4747
/// parse a URI suffix to convert text/plain mimeType to their actual web compatible mimeType.
4848
pub fn parse_from_uri(uri: &str) -> MimeType {
49+
Self::parse_from_uri_with_fallback(uri, Self::Html)
50+
}
51+
52+
/// parse a URI suffix to convert text/plain mimeType to their actual web compatible mimeType with specified fallback for unknown file extensions.
53+
pub fn parse_from_uri_with_fallback(uri: &str, fallback: MimeType) -> MimeType {
4954
let suffix = uri.split('.').last();
5055
match suffix {
5156
Some("bin") => Self::OctetStream,
@@ -61,15 +66,19 @@ impl MimeType {
6166
Some("svg") => Self::Svg,
6267
Some("mp4") => Self::Mp4,
6368
// Assume HTML when a TLD is found for eg. `wry:://tauri.app` | `wry://hello.com`
64-
Some(_) => Self::Html,
65-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
69+
Some(_) => fallback,
6670
// using octet stream according to this:
71+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
6772
None => Self::OctetStream,
6873
}
6974
}
7075

7176
/// infer mimetype from content (or) URI if needed.
7277
pub fn parse(content: &[u8], uri: &str) -> String {
78+
Self::parse_with_fallback(content, uri, Self::Html)
79+
}
80+
/// infer mimetype from content (or) URI if needed with specified fallback for unknown file extensions.
81+
pub fn parse_with_fallback(content: &[u8], uri: &str, fallback: MimeType) -> String {
7382
let mime = if uri.ends_with(".svg") {
7483
// when reading svg, we can't use `infer`
7584
None
@@ -78,8 +87,10 @@ impl MimeType {
7887
};
7988

8089
match mime {
81-
Some(mime) if mime == MIMETYPE_PLAIN => Self::parse_from_uri(uri).to_string(),
82-
None => Self::parse_from_uri(uri).to_string(),
90+
Some(mime) if mime == MIMETYPE_PLAIN => {
91+
Self::parse_from_uri_with_fallback(uri, fallback).to_string()
92+
}
93+
None => Self::parse_from_uri_with_fallback(uri, fallback).to_string(),
8394
Some(mime) => mime.to_string(),
8495
}
8596
}

tooling/cli/src/helpers/web_dev_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ async fn handler<T>(req: Request<T>, state: Arc<State>) -> impl IntoResponse {
145145

146146
file
147147
.map(|mut f| {
148-
let mime_type = MimeType::parse(&f, uri);
148+
let mime_type = MimeType::parse_with_fallback(&f, uri, MimeType::OctetStream);
149149
if mime_type == MimeType::Html.to_string() {
150150
let mut document = kuchiki::parse_html().one(String::from_utf8_lossy(&f).into_owned());
151151
fn with_html_head<F: FnOnce(&NodeRef)>(document: &mut NodeRef, f: F) {

0 commit comments

Comments
 (0)