2016-11-30 35 views
1

如何從js文件指向文件url?這是我想要實現的代碼片段。如何在phoenix中的js文件中指定摘要資產文件

const currentCacheName = "sample-app-v2"; 

self.addEventListener('install', (event) => { 
    const urlToCached = [ 
    '/', 
    '<%= static_path(@conn, "/css/app.css") %>', // Adding .eex on the js file won't work. 
    '<%= static_path(@conn, "/js/app.js") %>' 
    // Add fonts, icon, etc. 
    ]; 

    event.waitUntil(
    caches.open(currentCacheName).then(function(cache) { 
     return cache.addAll(urlToCached); 
    }) 
); 
}); 

self.addEventListener('activate', (event) => { 
    event.waitUntil(
    caches.keys().then((cacheNames) => { 
     return Promise.all(
     cacheNames.filter((cacheName) => { 
      return cacheName.startsWith('sample-app-') && cacheName != currentCacheName; 
     }).map((cacheName) => { 
      return cache.delete(cacheName); 
     }) 
    ); 
    }) 
); 
}); 

self.addEventListener('fetch', (event) => { 
    event.respondWith(
    caches.match(event.request).then(function(response) { 
     if(response) return response; 
     return fetch(event.request); 
    }) 
); 
}); 

我試着用serviceworker鳳凰,但我需要得到最新的摘要資產文件,它的工作。

回答

0

有幾種方法可以做這樣的事情。其中之一是將您的JS文件從靜態資產轉換爲模板 - 爲此創建路由,控制器,查看並將JS代碼移動到模板文件(例如 - web/templates/script/if下的script.js.eex你命名你的控制器像ScriptController和視圖是ScriptView)。在這種情況下,您可以在JS代碼中使用模板標籤,並根據需要從Web應用程序的根目錄提供腳本。這與向您的鳳凰應用添加新路線/頁面完全相似。此外,您應該禁用此類路由的防僞保護(從路由器中的管道中刪除:protect_from_forgery)。這樣做的缺點是你會失去這個腳本的所有JS管道,所以沒有像這樣的轉譯和其他東西。

相關問題