@@ -144,7 +144,12 @@ export const p5Versions = [
144144
145145export const currentP5Version = '1.11.5' ; // Don't update to 2.x until 2026
146146
147- export const p5SoundURLOld = `https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.3/addons/p5.sound.min.js` ;
147+ export const p5SoundURLOldTemplate =
148+ 'https://cdnjs.cloudflare.com/ajax/libs/p5.js/$VERSION/addons/p5.sound.min.js' ;
149+ export const p5SoundURLOld = p5SoundURLOldTemplate . replace (
150+ '$VERSION' ,
151+ '1.11.3'
152+ ) ;
148153export const p5SoundURL =
149154 'https://cdn.jsdelivr.net/npm/p5.sound@0.2.0/dist/p5.sound.min.js' ;
150155export const p5PreloadAddonURL =
@@ -168,8 +173,6 @@ export function P5VersionProvider(props) {
168173 const indexSrc = indexFile ?. content ;
169174 const indexID = indexFile ?. id ;
170175
171- const [ lastP5SoundURL , setLastP5SoundURL ] = useState ( undefined ) ;
172-
173176 // { version: string, minified: boolean, replaceVersion: (version: string) => string } | null
174177 const versionInfo = useMemo ( ( ) => {
175178 if ( ! indexSrc ) return null ;
@@ -207,14 +210,6 @@ export function P5VersionProvider(props) {
207210 // We only know for certain which one we've got if
208211 if ( usedP5Versions . length === 1 ) {
209212 const { version, minified, scriptNode } = usedP5Versions [ 0 ] ;
210- const replaceVersion = function ( newVersion ) {
211- const file = minified ? 'p5.min.js' : 'p5.js' ;
212- scriptNode . setAttribute (
213- 'src' ,
214- `https://cdn.jsdelivr.net/npm/p5@${ newVersion } /lib/${ file } `
215- ) ;
216- return serializeResult ( ) ;
217- } ;
218213
219214 const p5SoundNode = [
220215 ...dom . documentElement . querySelectorAll ( 'script' )
@@ -232,7 +227,9 @@ export function P5VersionProvider(props) {
232227 const newNode = document . createElement ( 'script' ) ;
233228 newNode . setAttribute (
234229 'src' ,
235- version . startsWith ( '2' ) ? p5SoundURL : p5SoundURLOld
230+ version . startsWith ( '2' )
231+ ? p5SoundURL
232+ : p5SoundURLOldTemplate . replace ( '$VERSION' , version )
236233 ) ;
237234 scriptNode . parentNode . insertBefore ( newNode , scriptNode . nextSibling ) ;
238235 }
@@ -250,6 +247,31 @@ export function P5VersionProvider(props) {
250247 return serializeResult ( ) ;
251248 } ;
252249
250+ const replaceVersion = function ( newVersion ) {
251+ const file = minified ? 'p5.min.js' : 'p5.js' ;
252+ scriptNode . setAttribute (
253+ 'src' ,
254+ `https://cdn.jsdelivr.net/npm/p5@${ newVersion } /lib/${ file } `
255+ ) ;
256+
257+ if ( p5SoundNode ) {
258+ if ( version . startsWith ( '2.' ) !== newVersion . startsWith ( '2.' ) ) {
259+ // Turn off p5.sound if the user switched from 1.x to 2.x
260+ setP5Sound ( false ) ;
261+ } else {
262+ // Replace the existing p5.sound with the one compatible with
263+ // the new version
264+ setP5SoundURL (
265+ version . startsWith ( '2.' )
266+ ? p5SoundURL
267+ : p5SoundURLOldTemplate . replace ( '$VERSION' , newVersion )
268+ ) ;
269+ }
270+ }
271+
272+ return serializeResult ( ) ;
273+ } ;
274+
253275 const p5PreloadAddonNode = [
254276 ...dom . documentElement . querySelectorAll ( 'script' )
255277 ] . find ( ( s ) => s . getAttribute ( 'src' ) === p5PreloadAddonURL ) ;
@@ -294,14 +316,13 @@ export function P5VersionProvider(props) {
294316
295317 return {
296318 version,
319+ isVersion2 : version . startsWith ( '2.' ) ,
297320 minified,
298321 replaceVersion,
299322 p5Sound : ! ! p5SoundNode ,
300323 setP5Sound,
301324 setP5SoundURL,
302325 p5SoundURL : p5SoundNode ?. getAttribute ( 'src' ) ,
303- lastP5SoundURL,
304- setLastP5SoundURL,
305326 p5PreloadAddon : ! ! p5PreloadAddonNode ,
306327 setP5PreloadAddon,
307328 p5ShapesAddon : ! ! p5ShapesAddonNode ,
0 commit comments