I have a bit of code that looks like this:
string myAssetUrl = “http://whatever.com/something?arg1=val1&arg2=val2”;
AssetReference asset = AssetReference.getOrCreateFromUrl(myAssetUrl);
asset.loadAssetAsync();
But at runtime, instead of requesting the value in myAssetUrl
, a value equal to myAssetUrl + “?v=12345678"
is requested instead.
The implementation of AssetReference.getOrCreateUrl(url: string, context?: Context)
should be modified to detect when a query string is already in the url
argument, appending the additional versioning argument instead of creating and appending an entire query string.
Hi Aaron, thanks for reaching out. The issue is that the current implementation of AssetReference.getOrCreateUrl doesn’t check if your provided URL already contains a query string. As a result, when you pass a URL like “http://whatever.com/something?arg1=val1&arg2=val2”, it simply appends another query string starting with “?” (e.g. “?v=12345678”), which yields an invalid URL with two question marks.
A straightforward fix is to modify the method so that it checks whether a “?” is already present in the URL. If it is, you should append your additional versioning parameter using an ampersand (“&”); if not, use a question mark (“?”). For instance, you could adjust it like this:
export class AssetReference {
// Assuming context might have a version defined. Otherwise, fallback to a default.
static getOrCreateUrl(url: string, context?: { version?: string }): string {
const version = context?.version || "12345678";
// Check if the URL already has a query string.
if (url.includes('?')) {
return `${url}&v=${version}`;
}
return `${url}?v=${version}`;
}
}
Using the URL API is another robust alternative, especially if you need to manage multiple parameters:
export class AssetReference {
static getOrCreateUrl(url: string, context?: { version?: string }): string {
const version = context?.version || "12345678";
try {
const parsedUrl = new URL(url);
parsedUrl.searchParams.set("v", version);
return parsedUrl.toString();
} catch {
// Fallback for environments where the URL API isn't available
return url.includes('?') ? `${url}&v=${version}` : `${url}?v=${version}`;
}
}
}
This change ensures that when your asset URL already contains query parameters, the version parameter is appended correctly rather than replacing or invalidating the existing query string. This approach should resolve the error you’re encountering.
Let me know if you need further assistance or additional examples!
1 replies left - Become PRO for more replies.
thread_IyDUhAb2S0o6EQpYIYctGKNm
Reply to this post directly if you need further help (bottom right button)
I agree, Mr. Bot, but I don’t have access to the implementation of that function; it’s in Needle’s core somewhere.
Hi Aaron, I understand your situation. If you don’t have access to modify the Needle Engine’s core implementation, the best approach would be to report the issue to the Needle team. They can handle it by updating the AssetReference.getOrCreateUrl
function as needed.
You might want to consider creating a bug report, detailing the issue you’ve described, and referencing your specific example code. This will help them reproduce the problem and implement a fix.
If you need, I can guide you on how to draft that report effectively or how to reach out to them. Let me know how you would like to proceed!
0 replies left - Become PRO for more replies.
thread_IyDUhAb2S0o6EQpYIYctGKNm
Reply to this post directly if you need further help (bottom right button)
Hi, thanks for the report. This seems indeed wrong
We’re looking into it
Hello Aaron,
it should be fixed in the next release. You can double check here: Needle-engine - Vite Template (duplicated) - StackBlitz (this is using a pre-release version 4.7.0-next.598a777)
Great! Thank you! Do you have a sense of when the next release will drop?
This week, probably tomorrow
1 Like
Could you try updating to Needle Engine 4.7.1?