ASP.Net Core PWA Cache Query

A. Jones

Asp.Net Core 2.2. MVC App which I am trying to turn into a Progressive Web App (PWA) using Mads Kristensen's WebEssentials.AspNetCore.PWA Package https://github.com/madskristensen/WebEssentials.AspNetCore.ServiceWorker and I am having some issues ensuring certain pages work offline.

The main functionality of the app involves a series of questions being presented to the user with the responses saved to a database. This view is populated by a ViewModel which queries the database in order to display the correct information.

My question is, as the database content will rarely change, am I able to cache the query result so the entire app can work offline? And if so, what is the best practice to do so?

My default map route is controller/action/id so when trying to utilize routesToPreCache in appsettings.json I am unable to include the id for the newly created 'incident' so it doesn't work - am I missing a simple trick or is the solution much more complex?

My controller for the view is as follows:

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);
    }
SRQ Coder

What you want to do can certainly be done. However, it will require a moderate amount of 'heavy lifting'

Mads PWA utility works great, but it does shield you somewhat from the service worker and manifest files.

You'll need to set the manifest to cache the 'questions' page. This will tell the PWA to go ahead and cache the page automatically on the user's device without the user ever having visited it. That way when they are offline, it will appear when they eventually browse to that page.

However, if the user is offline, submitting data by the user involves two things: storing the response values to local storage (not too difficult), and then syncing up the user's browser local storage by triggering an event when the device is online and then sending that data to the remote (your) database (rather involved, hence difficult).

Hope that helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related