【问题标题】:ASP.Net Core PWA Cache QueryASP.Net Core PWA 缓存查询
【发布时间】:2019-02-06 00:00:51
【问题描述】:

Asp.Net Core 2.2。我正在尝试使用 Mads Kristensen 的 WebEssentials.AspNetCore.PWA 包 https://github.com/madskristensen/WebEssentials.AspNetCore.ServiceWorker 将其变成渐进式 Web 应用程序 (PWA) 的 MVC 应用程序,我在确保某些页面脱机工作时遇到了一些问题。

该应用的主要功能包括向用户提出一系列问题,并将回答保存到数据库中。此视图由 ViewModel 填充,该 ViewModel 查询数据库以显示正确的信息。

我的问题是,由于数据库内容很少更改,我是否可以缓存查询结果,以便整个应用程序可以离线工作?如果是这样,这样做的最佳做法是什么?

我的默认地图路线是控制器/动作/id,所以当尝试在 appsettings.json 中使用 routesToPreCache 时,我无法包含新创建的“事件”的 id,所以它不起作用 - 我错过了一个简单的技巧还是解决方案更复杂?

我的视图控制器如下:

public class PathwayController : Controller
{
    private readonly ApplicationDbContext _context;

    public PathwayController(ApplicationDbContext context)
    {
        _context = context;
    }

    public ActionResult Pathway(int? id, int? index)
    {
        int currentIndex = index.GetValueOrDefault();
        if (currentIndex == 0)
        {
            ViewBag.NextIndex = 1;
        }
        else
        {
            ViewBag.NextIndex = index + 1;
        }

        PathwayViewModel path = new PathwayViewModel();
        var incident = _context.Incident
            .Include(i => i.IncidentType)
            .FirstOrDefault(m => m.Id == id);
        path.Incident = incident;
        var stageConditions = _context.StageCondition.Where(x => x.IncidentTypeId == incident.IncidentTypeId)
            .Include(s => s.Stage)
            .Include(o => o.Stage.Outcome)
            .OrderBy(x => x.Id)
            .Skip(currentIndex)
            .Take(1)
            .FirstOrDefault();

        path.StageCondition = stageConditions;

        // Validation
        var stageConditionTest = _context.StageCondition.Where(x => x.IncidentTypeId == incident.IncidentTypeId);

        ViewBag.MaxIndex = stageConditionTest.Count();

        //save stage to incident
        incident.StageId = stageConditions.Stage.Id;

        _context.Update(incident);
        _context.SaveChanges();

        return View(path);
    }

【问题讨论】:

    标签: c# asp.net-core progressive-web-apps


    【解决方案1】:

    你想做的事当然可以做到。但是,这将需要适度的“繁重”

    Mads PWA 实用程序效果很好,但它确实在一定程度上保护了您免受服务工作者和清单文件的影响。

    您需要将清单设置为缓存“问题”页面。这将告诉 PWA 继续并将页面自动缓存在用户的设备上,而无需用户访问它。这样,当他们离线时,它会在他们最终浏览到该页面时出现。

    但是,如果用户离线,则用户提交数据涉及两件事:将响应值存储到本地存储(不太难),然后通过在设备离线时触发事件来同步用户的浏览器本地存储在线,然后将该数据发送到远程(您的)数据库(相当复杂,因此很困难)。

    希望对您有所帮助。

    【讨论】:

    • 您好,谢谢!尝试缓存“问题”页面时,我遇到了一些问题。我的问题页面与“事件”相关联,并在创建新事件时加载 - 因为我的地图路线是控制器/操作/ID,我无法缓存特定页面,并且仅在尝试通用页面“控制器/问题”时控制台显示错误“Uncaught (in promise) TypeError: Request Failed”。检查这一点,它似乎发生在页面不存在时。
    猜你喜欢
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2017-03-15
    • 2020-03-24
    • 2012-01-16
    • 2021-06-13
    相关资源
    最近更新 更多